簡體   English   中英

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

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

如果我嘗試使用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

您需要在執行空驗證之前分配創建日期:

<?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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM