Phalcon\Mvc\Model :: beforeCreate()方法

ava*_*sin 3 php phalcon

如果我尝试使用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';
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器动作上下文

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

twi*_*tra 16

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

<?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';
    }
}
Run Code Online (Sandbox Code Playgroud)