Moose Triggers Not Firing When Using DBIX::Class
Asked Answered
W

2

6

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');
Warrenwarrener answered 5/3, 2010 at 21:22 Comment(1)
Seems like DBIC's auto-generated methods are overwriting Moose's auto-generated methods. The point of Moose is to prevent this, but it can't do that when something doesn't use Moose. Perhaps you just want an "around" modifier.Actinozoan
B
2

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.

Beaker answered 27/8, 2010 at 8:15 Comment(0)
B
-3

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

Baerman answered 6/3, 2010 at 9:34 Comment(1)
Hopefully not, because then the program would die at compile time.Actinozoan

© 2022 - 2024 — McMap. All rights reserved.