[Dbix-class] overriding create [was role of result class? ]
Richard Jolly
richardjolly at mac.com
Thu Mar 9 09:01:42 CET 2006
On 8 Mar 2006, at 20:40, Justin Guenther wrote:
> On 3/8/06, Richard Jolly <Richard.Jolly at bbc.co.uk> wrote:
>> It wouldn't work for my situation, as I want to divvy the arguments
>> given to create between a couple of other classes. By the time insert
>> was called it would have died with a 'no such column' error.
>
> I had a very similar problem. I created my own ResultSet class,
> overriding new_result() and create(). The new_result() method calls
> $self->result_source->result_class->new(@_) and sets the new object's
> resultsource to $self->result_source. Create() simply returns
> $self->new_result(@_)->insert().
>
> Then, I overrode new() and insert() in my Schema classes, catching the
> extra args and dealing with them manually.
>
> Hope that helps... I can post some example code later if you want, I
> have a meeting to go to or I would now.
I'd love to see code. That sounds like quite a bit of work to override
one method.
I'm not sure I get conceptually why create exists on ResultSets. It
looks like a constructor on a result class (which for better or worse I
still think of as defining a row-like object in most cases). But if I
understand correctly both of the following are identical, and frowned
upon (because you lose the possibility of different connections per
schema object):
$schema->class('Foo')->create(...)
Foo->create();
The $schema->storage object seems to serve a similar purpose to
Rose::DB's DB object (see "cross database migration"):
http://search.cpan.org/~jsiracusa/Rose-DB-Object-0.681/lib/Rose/DB/
Object/Tutorial.pod#A_brief_digression%3A_database_objects
But in that case you can easily set it on the result_class/row objects.
Something like:
# pseudo code with conjectural syntax
my $staging_schema = Schema->connect($staging_dsn);
my $production_schema = Schema->connect($production_dsn)
# make one in staging, and one on production
my $foo = Foo->new( $values );
$foo->schema( $staging_schema );
$foo->insert;
$foo->schema( $production_schema );
$foo->insert;
Finally, how can you do ->create on a resultset that does more than
simply describe a table? Isn't that going to always fail?
Am I beginning to understand?
Richard
More information about the Dbix-class
mailing list