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

StackOverflow https://stackoverflow.com/questions/22855026

  •  27-06-2023
  •  | 
  •  

Question

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top