[Dbix-class] role of result class?
Jess Robinson
castaway at desert-island.demon.co.uk
Tue Mar 7 12:52:45 CET 2006
On Tue, 7 Mar 2006, Richard Jolly wrote:
> Hi,
>
> I'm trying to understand the role of the result_class, and I have what
> I think are naive questions.
>
> In a schema with
>
> My::Schema::Artist
>
> ...
>
> my $schema->resultset('Artist')->search();
> My::Artist->search();
>
> Are the last two lines identical? Why is the first the preferred syntax?
Whether those two are identical or not, depends on how you connected, if
you just do;
My::Schema->connection('dbi:Foo' .. );
Then there is just one global connection, and you can call search on your
class names, if however you use:
my $schema = My::Schema->compose_connection, or ->connect,
then the information about the connecion/DB is stored in the $schema
object (in $schema->storage), and My::Artist->search will not work at all.
> Secondly, when over riding resultset methods for a specific class I
> believe the new methods get defined in My::Schema::Artist. But you
> could put them in My::Artist as well, and they would just sit slightly
> higher up the MRO. Does anyone actually put methods in the result class
> file?
No, adding methods to My::Schema::Artist or My::Artist will only make them
available to Row objects (DBIx::Class::Row).
The difference is this:, If you run a search in scalar context, what you
get is a ResultSet object, which will not have any of the methods you put
in your individual table classes (My::Artist etc.), but does have
convenience methods like ->count (how many items were returned) and so on.
Calling ->next on that result set, or calling search in list context, will
get you Row objects, which will each represent a row of the result, and
will be inherited from your My::Artist/table classes, and thus inherit
those methods.
So if you want to add methods to a search result as a whole, you need to
create your own result_class ..
I hope that makes sense,
Jess
More information about the Dbix-class
mailing list