avasin
avasin

Reputation: 9726

Phalcon\Mvc\Model::beforeCreate() method

If i try to save model with date_created field defined in beforeCreate() method, it does not save it:

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

Upvotes: 3

Views: 4042

Answers (1)

twistedxtra
twistedxtra

Reputation: 2699

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

Upvotes: 16

Related Questions