开发者

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 act开发者_JS百科ion 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();


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜