Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

created_at and validation

I've come across inconsistent behaviour between docs and actual implementation. For assigning date here (https://phalcon-php-framework-documentation.readthedocs.org/en/latest/reference/models.html#implementing-events-in-the-model-s-class) is advised to use either event manager

public function beforeCreate()
    {
        //Set the creation date
        $this->created_at = date('Y-m-d H:i:s');
    }

or Behaviors

public function initialize()
    {
        $this->addBehavior(new Timestampable(
            array(
                'beforeCreate' => array(
                    'field' => 'created_at',
                    'format' => 'Y-m-d'
                )
            )
        ));
    }

But in practice it doesn't work because of the failed validation! You have to use either

public function beforeValidationOnCreate()
{
    $this->created_at = date('Y-m-d H:i:s');
}

or

protected function _preSave()
{
    $this->created_at = date('Y-m-d H:i:s');
}


5.6k
edited Oct '14

Can I skip the whole validation when I save data using Phalcon\Mvc\Model ? I prefer to use standalone validation component so this kind of validation on the model side is kind of redundant!

I'm asking because following code doesn't skip validation

public function validation()
{
    return true;
}


84.1k
Accepted
answer

The example works with 'beforeCreate' because the column is not marked as "not null" in the mapped table. Of course, if the column is marked as "not null" you must use "beforeValidationOnCreate".

You can disable the automatic validation this way:

\Phalcon\Mvc\Model::setup(array(    
    'notNullValidations' => false
));

http://docs.phalconphp.com/en/latest/reference/models.html#disabling-enabling-features



5.6k

If I understand correctly in case

\Phalcon\Mvc\Model::setup(array(    
    'notNullValidations' => false
));

if I don't assign any value to a property (e.g. leave it intact) then it's NOT validated, am I right?



84.1k

If you disable the automatic validation Phalcon will not validate fields marked as "not null" in the mapped table, However if the developer allow pass a null value to a column that does not allow null values a PDOException will be thrown due to the constraint violations in the database server, so the developer must be aware of that to avoid showing error pages to final users.



1.8k
edited Oct '14

I prefer to use not null on created_at. Ok, it works with beforeValidationOnCreate, but what about not null columns with a default value?

create table example (
   x int not null default 1
);

Validation should be aware of this!



84.1k

@posthly You must explicitly tell the ORM to use the default value:

<?php

use Phalcon\Mvc\Model,
    Phalcon\Db\RawValue;

class Robots extends Model
{
    public function beforeValidationOnCreate()
    {
        if (!$this->createdAt) {
            $this->createdAt = new RawValue('default');
        }
    }
}