[Catalyst] $c->user->some_relationship always retrives user id from the db

Jay K jayk at ion0.com
Thu Feb 14 18:06:09 GMT 2008


> So why do I need to recovery the whole user row for every request?
> I could use
> $user_id = $c->session->{user_id};
> @albums = $c->model('albums')->search({ user_id => $user_id });
> as Mitchell suggested but I'd prefer to fetch the data by using $c-
> >user because its the way one should do it.
>
> might it be possible to store the user row in the session at login
> and recover $c->user from the session on every request?
> I know this might break if I change something in the user row.


Hi again Moritz,

Matt is correct - Because nobody has written the 'store everything in
the session' bit yet.   I have a plan to change the DBIx::Class store
so that it will save the row info into the session - but I have not
done so yet.   The next release will include that functionality, but
at this time I can't tell you when exactly that will be.  I'm going to
try to get it done this weekend.

In anticipation of that - I can tell you that you should use $c->user-
 >get('fieldname');  When my code is complete, that will be the 'don't
hit the database' way of accessing row information.   Accessing $c-
 >user->obj or $c->user->get_object will trigger the DB hit.  And
since $c->user->fieldname just relays to $c->user->get_object-
 >fieldname - the shortcut method won't work either if you want to
avoid the db hit.

Jay

---
"May we not return to those scoundrels of old, the illustrious
founders of superstition and fanaticism, who first took the knife from
the altar to make victims of those who refused to be their disciples."
- Voltaire





More information about the Catalyst mailing list