[Catalyst] Where to put common functions & how to get user id

Mitch Jackson perimus at gmail.com
Tue May 27 14:43:33 BST 2008

> My users table contains following columns:
> * user_id
> * username
> * password
> * first name, last name etc.......

the 'id' method is provided by DBIx::Class::PK, and is not one of your columns.
have you tried $c->user->user_id instead of $c->user->id?

> my $thisuser = $c->model('MyDB::Users')->find({username => $c->user->id});
> my $thisuser_id = $thisuser->user_id;

> Is there a simpler way to do it? I need current user's ID in almost every
> controller all the time...

The user object is already loaded by the authentication plugin.  You
don't have to fetch it from the database again.

$thisuser = $c->user->obj;
$thisuser_id = $c->user->user_id
$thisuser_id = $c->user->obj->user_id;

Hope this helps!

/Mitchell K. Jackson

More information about the Catalyst mailing list