【问题标题】:Phalcon\Mvc\Model::beforeCreate() methodPhalcon\Mvc\Model::beforeCreate() 方法
【发布时间】:2013-02-01 14:58:29
【问题描述】:

如果我尝试使用 beforeCreate() 方法中定义的 date_created 字段保存模型,它不会保存它:

class TestEntity extends Phalcon\Mvc\Model
{

    public function beforeCreate()
    {
        $this->date_created = date('Y-m-d H:i:s');
    }

    /**
     * Returns source table name
     * @return string
     */
    public function getSource()
    {
        return 'test_entity';
    }
}

控制器动作上下文:

$test = new TestEntity();
$test->name = 'test';
var_dump($contact->save()); // gives false
var_dump($contact->getMessages()); // says date_created is not defined

【问题讨论】:

  • test_entity 表中的字段是否名为 date_created?
  • @NikolaosDimopoulos 正如 twistedxtra 所说,有必要使用一点不同的方法 beforeValidationOnCreate()。谢谢你:)

标签: php phalcon


【解决方案1】:

您需要在执行空验证之前指定创建日期:

<?php

class TestEntity extends Phalcon\Mvc\Model
{

    public function beforeValidationOnCreate()
    {
        $this->date_created = date('Y-m-d H:i:s');
    }

    /**
     * Returns source table name
     * @return string
     */
    public function getSource()
    {
        return 'test_entity';
    }
}

【讨论】:

  • modified_at 怎么样?如果我想在更新记录时更新 modified_at?谢谢
  • @piavgh 您可以使用beforeUpdate() 方法 - 或beforeValidationOnUpdate() 方法,如果您要更改的字段将通过验证。你可以在这里找到更多信息:docs.phalconphp.com/en/latest/reference/models.html
猜你喜欢
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
相关资源
最近更新 更多