[Dbix-class] Use of ->resultset mandatory?

Christopher H. Laco claco at chrislaco.com
Mon Oct 30 21:52:47 GMT 2006


Steven Mackenzie wrote:
> Christopher H. Laco wrote:
> =

>> Steven Mackenzie wrote:
>>  =

>>
>>> Why is there no accessor in the schema class for each table? Wouldn't it
>>> be obvious to type
>>> $schema->MyTableClass->search( .. )
>>>
>>> Not quite as brief as the CDBI style, but resultset('') starts to hurt
>>> my fingers (especially the punctuation parts of it ...)
>>>
>>> Steven
>>>    =

>>>
>> Don't always assume that table eq 'tableclass', or even schema class.
>> The three need not be related by name in any way.
>>
>>  package My::Schema::Foo;
>>  __PACKAGE__->table('stuff');
>>  __PACKAGE__->source_name('MyBar');
>>
>>  $schema->resultset('MyBar')->search;
>>
>>
>>  package My::Schema::Foo2;
>>  __PACKAGE__->table('stuff');
>>  __PACKAGE__->add_column('evenmore');
>>  __PACKAGE__->source_name('MyBest');
>>
>>  $schema->resultset('MyBest')->search;
>>
>>
>> You can use two resultsource classes against the same table(), kind of
>> like inherited tables.
>>
>>  =

>>
> Aha, I see. In the example above, the base table in the DB has a full
> set of columns, and My::Schema::Foo presents one subset of them, and
> My::SchemaFoo2 exposes another set of them? It's possible that my head
> wouldn't be able to navigate three layers of indirection (source
> name->package name->table name), but nothing forces me to write code
> that uses it, so that's OK  :-)
> =

> In the example above, if the same row from the base class is
> instantiated as a Foo and a Foo2, are the column values synchronised? If

No. There is no CDBI-like magic cache where two objects point to the
same row in memory.

> not, then it's more correct to stick to a single class per table surely?

Generally, yes. Just that things like source_name make more complicated
things possible.


> =

> In my quest to avoid typing resultset(''), is there a reason why I can't
> have source_name create accessors in the $shema object, eg,
>   $schema->MyBest->search;
> =

> =


There's no reason you can't put this in your schema class:

sub MyBest {
   return shift->resulset('MyBest');
};

As for the above examples, they're not the best. The main point is, the
name used in resultset() could be the class name or the source_name. Any
number of classes can use the same table, so just adding shortcuts for
the table name is futile. People can set the source_name to abstract
away from the class names, so when the class names change, the
resultset() calls don't have too....and various other reasons...

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://jules.scsys.co.uk/pipermail/dbix-class/attachments/20061030/92=
2e5781/signature.pgp


More information about the DBIx-Class mailing list