3
votes

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
1
Is the field in test_entity table called date_created? - Nikolaos Dimopoulos
@NikolaosDimopoulos it was necessary to use little bit different method, beforeValidationOnCreate(), as twistedxtra said. Thank you :) - avasin

1 Answers

16
votes

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