Reputation: 1068
I started to use CakePHP3.0 by mere curiosity. To familiarize myself with the new features of CakePHP3.0, I followed the blog tutorial in official website(http://book.cakephp.org/3.0/en/tutorials-and-examples/blog/blog.html). What I did was just simply copy and past of the source code there. Everything works fine, EXCEPT FOR fields "created" and "modified" not being saved. They just stay NULL. I have confirmed that this feature works fine in CakePHP 2.4.6. Below is the table definition and function add() for the blog tutorial.
CREATE TABLE articles (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
public function add(){
$article = $this->Articles->newEntity($this->request->data);
if($this->request->is("post")){
if($this->Articles->save($article)){
$this->Session->setFlash("Success!");
return $this->redirect(["action"=>"index"]);
}
$this->Session->setFlash("Fail!");
}
$this->set(compact("article"));
}
Upvotes: 8
Views: 5735
Reputation: 2597
In Part 2 of the Blog Tutorial, it appears you missed the Creation of the Articles Model: http://book.cakephp.org/3.0/en/tutorials-and-examples/blog/part-two.html#create-an-article-model
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
class ArticlesTable extends Table {
public function initialize(array $config) {
$this->addBehavior('Timestamp');
}
}
The inclusion of the 'Timestamp' behaviour is what controls these timestamp fields and keeps them up to date.
Upvotes: 0
Reputation: 25698
You need to add the TimestampBehavior in 3.0.
https://github.com/cakephp/cakephp/blob/3.0/src/Model/Behavior/TimestampBehavior.php
Upvotes: 9