by Daniel | hits(6539)
In this blog posts I'll introduce you to the WhoDidIt Model Behavior for CakePHP!
Building a CMS like application I wanted to keep track of:
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.
On my public repository for CakePHP snippets on GitHub@danfreak
First of all you need a table with two fields
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:
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:
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
view/hide comments | add comment
powered by 4webby.com