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

Matt S Trout dbix-class at trout.me.uk
Thu May 29 12:04:40 BST 2008


On Tue, May 27, 2008 at 08:13:06AM +0200, jakac wrote:
> Hello!
> 
> Can someone please explain where should I put common functions such as
> "action logging" (my app is designed so that it actually logs any 
> inserts/updates
> of data)?
> 
> My table for logging this data is called "worklog" and contains 
> following columns:
> * id
> * date
> * time
> * user_id (integer)
> * action (text)
> 
> in my older (non-catalyst) application I had a common function defined as:
> 
> sub workLog {
>   my ($dbh,$user_id,$action) = @_;
>   return $dbh->do("insert into worklog user_id,action values 
> ('$user_id','$action');");
> }
> 
> And whenever I needed to log something I just used a call like:
> &workLog($dbh,$user_id, ' User updated table foo column bar');

Using &name for calling functions when you already know the name is perl 4.

Do NOT, EVER do it in perl 5.
 

> Also the other thing is - how to get active user's ID? When I 
> authenticate my user
> using Authentication plugin (using $c->login($username,$password)) the 
> $c->user->id
> contains that user's username and not his ID.

In Catalyst auth terms the 'id' is what the user uses to identify to the
app. It doesn't know about the rest of your DB schema.
 
> My users table contains following columns:
> * user_id
> * username
> * password
> * first name, last name etc.......
> 
> user authenticates using his username&password just like in "Catalyst 
> authentication tutorial"
> but my application always needs user's ID so I always have to search for 
> this user in the
> database...

No you don't.

$c->user->obj returns the DBIC object.

So $c->user->obj->user_id will give you the user_id.

Although really, you shouldn't need that in your controller. Why do you
think you need a model-specific integer in code that's not supposed to
understand the model?

-- 
      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