Dan Berlyoung
Dan Berlyoung

Reputation: 1689

How to get the model and action name of the calling action

I have a log feature in my project that tracks all changes to a couple of different models. Say, whenever a user or an admin edits an account. Or when a user creates a new record. All those actions get recorded in the log table.

I would like to have the log model retrieve the model name and action name by itself in a beforeSave action when called to save a new record. I call it now from a controller action making the save like this:

$this->Log->save(array(
    'user'=>$this->Auth->user('id'), 
    'model'=>$this->name, 
    'action'=>$this->action));

I would love to be able to shorten it down to:

$this->Log->save();

Upvotes: 0

Views: 1819

Answers (2)

Dan Berlyoung
Dan Berlyoung

Reputation: 1689

I found alkerman's wonderful LogableBehavior and it is working like a charm. No need to reinvent the wheel.

Upvotes: 1

Leo
Leo

Reputation: 6571

I think I'd be inclined to either write a method on app_controller such as

saveLog() which calls

$this->Log->save($uma_array)

or simplify your existing construct by doing

$this->Log->save($this)

and untangling the data inside the method.

EDIT:

Of course, the best way to do this would be to use observable behaviour using teknoid's pattern: http://nuts-and-bolts-of-cakephp.com/2009/08/10/observer-pattern-the-cakephp-way/ It's not that difficult to implement.

Upvotes: 0

Related Questions