gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:54:53 29 4
gpt4 key购买 nike

如果我尝试使用在 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 上下文:

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

关于php - Phalcon\Mvc\Model::beforeCreate() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649030/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com