Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fields "created" and "modified" are not set automatically in CakePHP3.0.0(dev preview 2)

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"));
}
like image 759
hitochan Avatar asked Apr 04 '14 06:04

hitochan


1 Answers

You need to add the TimestampBehavior in 3.0.

https://github.com/cakephp/cakephp/blob/3.0/src/Model/Behavior/TimestampBehavior.php

like image 76
floriank Avatar answered Oct 19 '22 17:10

floriank