Category: Models & ORM

Q: Model fields created and modified are not saved to mysql

Model fields `created` and `modified` should be saved to database on Model.save. Every fields expect for `created` and `modified` are working fine (not null), which should be working on every save or update action. The following code is the table structure and posts/add action.

Table structure for posts:

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
--  Table structure for `posts`
-- ----------------------------
CREATE TABLE `ac_posts` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  `content` text,
  `sticky` smallint(5) unsigned DEFAULT '0',
  `sort` smallint(5) unsigned DEFAULT '0',
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

SET FOREIGN_KEY_CHECKS = 1;

In PostsController.php

//add new post
public function add() {

  if ($this->request->is('post')) {
    
      $entity = $this->Posts->newEntity($this->request->data);
      if ($this->Posts->save($entity)) {
           $this->Flash->success('Post created!', ['key' => 'addPost']);
           $this->redirect(['action' => 'lists']);
      } else {
           $this->Flash->error('Something wrong.', ['key' => 'addPost']);
           $this->redirect(['action' => 'add']);
      }
  }
}
save
3 Answers

Did you add a timestamp behavior to PostsTable.php ?

<?php

namespace App\Model\Table;

use Cake\ORM\Table;

class PostsTable extends Table {

  public function initialize(array $config) {

    $this->table('posts');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp', [
        'events' => [
            'Model.beforeSave' => [
                'created' => 'new',
                'modified' => 'always',
             ]
        ]
    ]);

  }
}

check this link.

In PostsController.php

//add new post
public function add() {

  if ($this->request->is('post')) {

      //get server's current time
      $this->request->data['created'] = date('Y-m-d H:i:s');
    
      $entity = $this->Posts->newEntity($this->request->data);
      if ($this->Posts->save($entity)) {
           $this->Flash->success('Post created!', ['key' => 'addPost']);
           $this->redirect(['action' => 'lists']);
      } else {
           $this->Flash->error('Something wrong.', ['key' => 'addPost']);
           $this->redirect(['action' => 'add']);
      }
  }
}
Kristi
Kristi

137

Use Cake's Timestamp behavior for updating `created` and `modified` automatically.

<?php
namespace App\Model\Table;
use Cake\ORM\Table;

class PostsTable extends Table {
  public function initialize(array $config) {
    $this->addBehavior('Timestamp');
  }
}

Created: 27 Sep '16

Last Reply: 27 Sep '16

Replies: 3

Views: 573

Votes: 0

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.


Join Now Tour

Download Cakephp

Start baking your own CakePHP application!


Cakephp All Versions