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?

1 Answer 1

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!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.