Moose method modifiers on DBIx::Class::Schema models in Catalyst
Asked Answered
F

1

9

For any given result class MySchema::Result::Foo (built from default schema loader generated syntax which uses Moose/MooseX::nonmoose)

If I add a BUILDARGS method wrapper to sanitize the constructor data for a row like so:

package MySchema::Result::Foo;
use Moose;
use MooseX::NonMoose;
[etc ..]

around 'BUILDARGS' => sub {
   my $orig = shift;
   my $class = shift;
   delete $_[0]->{not_a_real_column};
   return $class->$orig(@_);
};

It works when using the schema directly. For example the following works as expected: A new row object is created with real_column=>'value' and not_a_real_column removed before ->new is called

use MySchema;
my $s = MySchema->connect('dbi:blahblahblah');
$s->resultset('Foo')->new({ real_column=>'value', not_a_real_column=>'some other thing' }); #win

However, when using the same schema via Catalyst::Model::DBIC::Schema the order is different. The following fails when trying to create a new Foo row object because not_a_real_column is invalid. In other words the arguments to new are not run through BUILDARGS before ->new is called.

$c->model('MySchemaModel')->resultset('Foo')->new({ real_column=>'value', not_a_real_column=>'some other thing' }); #fails

Interestingly enough, if I wrap around 'new' => sub{} instead of around 'BUILDARGS' => sub{} the behavior is the same in both cases, and works fine, but to my understanding Moose dogma states to never mess with new.

Anyone care to help me understand why this is the case, or if there's a better way?

Flinger answered 7/12, 2010 at 14:28 Comment(0)
F
1

I see, you are using MooseX::NonMoose.

Given that, I have a guess that you need to use FOREIGNBUILDARGS

around 'FOREGINBUILDARGS' => sub {
   my $orig = shift;
   my $class = shift;
   delete $_[0]->{not_a_real_column};
   return $class->$orig(@_);
};

"MooseX::NonMoose allows you to manipulate the argument list that gets passed to the superclass constructor by defining a FOREIGNBUILDARGS method."
http://metacpan.org/pod/MooseX::NonMoose

I really hope this works for you!

Frigg answered 9/12, 2011 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.