【问题标题】:Fields "created" and "modified" are not set automatically in CakePHP3.0.0(dev preview 2)CakePHP3.0.0(dev preview 2)中没有自动设置字段“created”和“modified”
【发布时间】:2014-05-16 07:24:00
【问题描述】:

出于好奇,我开始使用 CakePHP3.0。 为了熟悉 CakePHP3.0 的新特性,我跟着官网的博客教程(http://book.cakephp.org/3.0/en/tutorials-and-examples/blog/blog.html)。我所做的只是简单地复制和过去那里的源代码。 一切正常,除了“创建”和“修改”字段未保存。他们只是保持NULL。我已经确认此功能在 CakePHP 2.4.6 中运行良好。下面是博客教程的表定义和函数add()。

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

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    在博客教程的第 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');
        }
    }
    

    包含“时间戳”行为是控制这些时间戳字段并使它们保持最新的原因。

    【讨论】:

    【解决方案2】:

    【讨论】:

    • 谢谢!它工作得很好......我应该更彻底地用谷歌搜索它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 2012-01-26
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    相关资源
    最近更新 更多