I'm not sure that I understand correctly for what purpose Behaviors in Models are used. However I suppose, that the Timestampable behavior are using for autofilling DATETIME fields during creating or updaing a row. Like that:
$user->created_at = date('Y-m-d H:i:s');
If I understand it correctly I can remove this string (above) from controller and add this in model:
public function initialize()
{
$this->addBehavior(new Timestampable(array(
'beforeCreate' => array(
'field' => array('created_at', 'logined_at'),
'format' => 'Y-m-d H:i:s'
)
)));
}
Also in controller:
$user = new Users();
$user->initialize(); // Don't forget to call it to register behavior
But I can't get this work. I just post signup form and get: created_at is required logined_at is required
Yes, here is a part of my scheme:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL DEFAULT '',
`nickname` varchar(32) NOT NULL,
`password` varchar(64) NOT NULL,
`created_at` datetime NOT NULL,
`logined_at` datetime NOT NULL,
`karma` tinyint(4) NOT NULL DEFAULT '0',
`role` tinyint(1) NOT NULL DEFAULT '2',
`is_banned` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`,`nickname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I also tried to change this two fields to accept NULL, and it inserts, but this fields was NULL instead of current timestamp formatted to DATESTAMP...
So how to trick, or maybe I don't understand something?