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

Matt S Trout dbix-class at trout.me.uk
Thu Feb 14 20:19:17 GMT 2008


On Thu, Feb 14, 2008 at 11:06:09AM -0700, Jay K wrote:
> >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.

But then you can't follow even PK relationships without the db hit.

Which means the entire thing would be pointless. What you want to do
really is

  $class->new({ %saved_stuff, -result_source => $schema->source($class) });
  $new_obj->in_storage(1);

then it'll work "as if" you re-fetched the object (%saved_stuff being the
return of $obj->get_columns). Making stuff work with only the PK stashed
in the session is left as an exercise to the reader, but would probably
be best done using Data::Thunk and Data::Swap.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the Catalyst mailing list