简体   繁体   English

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

[英]Phalcon\Mvc\Model::beforeCreate() method

If i try to save model with date_created field defined in beforeCreate() method, it does not save it: 如果我尝试使用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';
    }
}

Controller action context: 控制器动作上下文

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

You need to assign the creation date before the null validation is performed: 您需要在执行空验证之前分配创建日期:

<?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';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Phalcon MVC模型异常 - Phalcon MVC model Exception Phalcon \\ Mvc \\ Model :: find()以及Phalcon \\ Mvc \\ Model \\ Query \\ Builder - Phalcon\Mvc\Model::find() together with Phalcon\Mvc\Model\Query\Builder Phalcon \\ Mvc \\ Model的afterFetch - Phalcon\Mvc\Model's afterFetch 消息“方法” issent”在模型“ Artist”上不存在未捕获的异常“ Phalcon \\ Mvc \\ Model \\ Exception” - Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'The method “issent” doesn't exist on model “Artist”' Phalcon 3:使用\\ Phalcon \\ Mvc \\ Model \\ Validator验证表单数据 - Phalcon 3: Validating form data using \Phalcon\Mvc\Model\Validator Phalcon 1.3.0使用Phalcon \\ Mvc \\ Model \\ Query来制作LIKE子句 - Phalcon 1.3.0 using Phalcon\Mvc\Model\Query to make LIKE clause 是否可以在一种Phalcon \\ Mvc \\ Model :: find()方法中指定两个极限值(来自,偏移)? - Is it possible to specify two limit values(from, offset) in one Phalcon\Mvc\Model::find() method? (Phalcon)如何从模型中的静态方法重定向(Phalcon \\ Http \\ Response) - (Phalcon) How to redirect (Phalcon\Http\Response) from static method in model “独立”模式下的Phalcon \\ Mvc \\ Model \\ Validator \\ Uniqueness - Phalcon\Mvc\Model\Validator\Uniqueness in “standalone” mode Phalcon \\ Mvc \\ Model-回滚失败的事务 - Phalcon\Mvc\Model - rolling back failed transaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM