2014-04-04 15 views
8

我開始只是好奇使用CakePHP3.0設置。 要使用CakePHP3.0的熟悉新功能我自己,我也跟着在官方網站(http://book.cakephp.org/3.0/en/tutorials-and-examples/blog/blog.html)的博客教程。我所做的只是在那裏複製和過去的源代碼。 一切工作正常,除了字段「創建」和「修改」不被保存。他們只是保持NULL。我已經確認這個特性在CakePHP 2.4.6中工作正常。以下是博客教程的表格定義和函數add()。字段「創建」和「修改」不會自動CakePHP3.0.0(開發預覽2)

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

回答

0

在博客教程的第2部分,看來你錯過了文章模型的生成: 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'); 
    } 
} 

「時間戳」行爲列入就是控制這些時間戳並保持它們的最新狀態。

+0

這工作對我來說,更多的信息:https://book.cakephp.org/3.0/en/tutorials-and-examples/blog/part-two.html –