We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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


8.4k
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;
}


98.9k
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
));

https://docs.phalcon.io/en/latest/reference/models.html#disabling-enabling-features



8.4k

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?



98.9k

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.



3.1k
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!



98.9k

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