4webby BLOG

01 03 2009

WhoDidIt behavior: monitor who has manipulated a record automagically in CakePHP

by Daniel | hits(6539)

TAGS: cakephp models whodidit behavior CMS

 

In this blog posts I'll introduce you to the WhoDidIt Model Behavior for CakePHP!

Why I developed it


Building a CMS like application I wanted to keep track of:

  • who did create a record
  • who did modify it


possibly this has to be done automagically adding to fields in the DB table "created_by" an "modified_by"!

The most straigthforward solution was to develope a model behavior.

Where can I get it?

On my public repository for CakePHP snippets on GitHub@danfreak

How it works


First of all you need a table with two fields

  • created_by
  • modified_by


This can be a demo for MySQL:

CREATE TABLE IF NOT EXISTS `posts` ( 
`id` int(11) NOT NULL auto_increment,
`content_news_category_id` int(2) NOT NULL,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`on_cover` tinyint(1) unsigned default NULL,
`published` tinyint(1) unsigned NOT NULL,
`publish_on` datetime default NULL,
`created_by` int(11) NOT NULL,
`modified_by` int(11) NOT NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
`hits` int(11) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `created_by` (`created_by`,`modified_by`),
KEY `published` (`published`),
KEY `publish_on` (`publish_on`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


The created_by, modified_by fields must be of INT type.

The you need to attach the behavior to the model (or AppModel if you want all your models to actAs WhoDidIt)

example:

class Post extends AppModel { 
var $name = 'Post';
var $actsAs = array('WhoDidIt');

}



The above example uses the default settings:

  • 'auth_session' the name of the Auth session default Auth
  • 'user_model'  the name of the User model (default User)
  • 'created_by_field' the name of the "created_by" field in DB (default 'created_by')
  • 'modified_by_field'  the name of the "modified_by" field in DB (default 'modified_by')
  • 'auto_bind'  automatically bind the model to the User model (see example below)


If you want to customize the settings simply specify them in the acts as:

class Post extends AppModel { 
var $name = 'Post';
var $actsAs = array('WhoDidIt'=>array('user_model'=>'MyUser',
'created_by_field'=>'built_by',
'modified_by_field'=>'affected_by'));

}


The above example will override the default settings using:

  • MyUser as User model instead of User
  • built_by as "created_by" field in the DB table
  • affected_by as "created_by" field in the DB table


By default the WhoDidIt Behavihor creates an association so that you can retrieve the creator/modifier detail and get a record structure as follows:

Array 
(

[Post] => Array
(

[id] => 12
[content_news_category_id] => 1
[title] => example post title
[content] => example post content
[on_cover] => 1
[published] => 1
[publish_on] => 2009-02-28 13:04:00
[created_by] => 1
[modified_by] => 1
[created] => 2009-02-28 13:18:23
[modified] => 2009-02-28 13:18:23
[hits] => 0
)


[CreatedBy] => Array
(

[id] => 1
[nome] => Myname
[cognome] => Mysurname
[username] => danfreak
[email] => xxx@xxxxxxx.com
[password] => xxxxxxxxxxxxxxxxxxxxxxxxxxx
[group_id] => 1
[created] => 2008-12-01 00:00:00
[modified] => 2008-12-08 11:57:02
[activation] =>
)


[ModifiedBy] => Array
(

[id] => 1
[nome] => Myname
[cognome] => Mysurname
[username] => danfreak
[email] => xxx@xxxxxxx.com
[password] => xxxxxxxxxxxxxxxxxxxxxxxxxxx
[group_id] => 1
[created] => 2008-12-01 00:00:00
[modified] => 2008-12-08 11:57:02
[activation] =>
)



If you don't want to create the above association on the fly, simply override the default settings on behavior binding as follows:

class Post extends AppModel { 
var $name = 'Post'; var $actsAs = array('WhoDidIt'=>array('auto_bind'=>false); }


I hope you will enjoy the WhoDidIt behavior!

Bake ON!

Daniel

 

Further Resources:

view/hide comments | add comment

4webby.com

Tags

powered by 4webby.com