8

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"));
}

2 Answers 2

9

You need to add the TimestampBehavior in 3.0.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It worked fine... I should have googled it more thoroughly.
0

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.

1 Comment

This works to me, more informations: book.cakephp.org/3.0/en/tutorials-and-examples/blog/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.