views:

292

answers:

2

I am new to Moose and am trying to use it with DBIx::Class. Basic DBIC querying and updating work find, but any trigger I attempt to write does not get executed when I modify an attribute.

use Modern::Perl;
use Data::Dumper;

my $schema = My::Schema->connect(<connect str>, <usr>, <psw>) or die $!;
my $rs = $schema->resultset('Isin')->search( sid => 3929 );
my $security_obj = $rs->first;
print $security_obj->isin, "\n";
$security_obj->isin('Test1Foo'); # <- expect to see FOO printed by trigger
print $security_obj->isin, "\n";

I expect to see the trigger for 'isin' print 'FOO', but nothing happens. If I strip out DBIx::Class from the package the trigger is executed as expected.

I suspect that DBIx::Class is setting the value in a way that prevents the trigger from firing.

Unfortunately, I haven't had much luck finding resources about using DBIx::Class with Moose. What I have written is mostly based on what I found at DBIx::Class and Moose.

Am I using DBIx::Class and/or Moose wrong? Is there a different ORM that I should be using with Moose?

The package with the trigger that won't fire:

package My::Schema::Result::Isin;

use DBIx::Class;
use Moose;
use Carp;
extends 'DBIx::Class';

has 'isin'   => ( is => "rw", isa => "Str", trigger => \&_mod_isin);
has 'sid'    => ( is => "ro", isa => "Int");

sub _mod_isin {
    print "FOO\n";
    return;
};

 no Moose;

__PACKAGE__->load_components('Core');

__PACKAGE__->table('isin');

__PACKAGE__->add_columns(
  isin  => { data_type => 'varchar2', size => 12 },
  sid   => { data_type => 'integer',  size => 6 },
);

__PACKAGE__->set_primary_key('isin');
A: 

Have you tried using writer => \&_mod_isin instead?

Kiffin
Hopefully not, because then the program would die at compile time.
jrockway
+1  A: 

First, you have the problem of extending a non-Moose class from within Moose. This is a problem because DBIx::Class doesn't inherit from Moose::Object, so you won't get the standard Moose methods like does. See Moose::Cookbook::Basics::Recipe11 for solving this problem.

Second, you have the bigger problem that you have two different sets of magic which are trying to create subroutines for you. You have Moose, whose magic creates isin and sid subroutines, and you have DBIx::Class, whose magic also creates isin and sid subroutines which replace the ones that Moose created.

You might want to compose in a Moose Role with an around modifier, as jrockway suggested.

Philip Potter