[Dbix-class] accessing ResultSet function from a related object
Matt S Trout
dbix-class at trout.me.uk
Thu Jul 5 01:26:02 GMT 2007
On Wed, Jul 04, 2007 at 05:47:05PM -0400, John Goulah wrote:
> I am having some trouble in both controller and template code accessing
> resultset functions off of chained/related objects (because from what I'm
> seeing they are not ResultSets but of the type
> MyApp::Model::MyAppDB::Tablename).
That's isa MyAppDB::Tablename, not MyAppDB::TablenameResultset or whatever.
> For example I can create a ResultSet object directly like:
> my $owner = $c->model('MyDB::User')->search({ user_id => $owner_id } );
>
> and then I can do for example:
>
> $owner->some_result_set_method()
If it's an RS, call the variable $owners or $owner_rs. Trust me that this will
make your life easier.
> but I can't do this if I want to chain off another object, such as:
>
> #not ok
> my $owner_id =
> $photo_details_rs->first()->owner->some_result_set_method();
>
> but...
>
> #this is fine, just accessing a field
> my $owner_id = $photo_details_rs->first()->owner->user_id;
The relationship must be returning an object, not a resultset then.
Don't confuse the Result (single object) and ResultSet (effectively a
virtual collection) objects. There's no reason you can't implement methods
on both but they are -not- the same thing.
This is why I religiously keep $owner versus $owner_rs coding conventions;
it makes it way easier to avoid getting confused :)
--
Matt S Trout Need help with your Catalyst or DBIx::Class project?
Technical Director Want a managed development or deployment platform?
Shadowcat Systems Ltd. Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/ http://www.shadowcatsystems.co.uk/
More information about the Dbix-class
mailing list