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

beforeValidationOnUpdate default bool value

Hi All

I have a model behavior using beforeValidationOnUpdate that i do not understand. I have a Mysql(5.6) table

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`var` TINYINT(1) NOT NULL DEFAULT '1'

My Phalcon(3.1.1) model has the following

...
public function beforeValidationOnCreate() {
  $this->var = TRUE;
}

public function beforeValidationOnUpdate() {
  $this->var = TRUE;
}
...

A record is created, when I run $model->create(). An error message var is required, when I test $model->update(). If i set $this->var = 1 and run $model->update(), then the validation is passed. Can anyone explain this or is this a bug? I would expect the same validation from both create and update using $this->var = TRUE;

Thanks in advance

Well, tbh it might be a bug and might not be. Depends who you will ask, var columns is supposed to be INT, you provided boolean ins model so that's why you have this error message(from not null validations), basically from this code:

if typeof value != "object" {
                                if !isset dataTypeNumeric[field] {
                                    if isset emptyStringValues[field] {
                                        if value === null {
                                            let isNull = true;
                                        }
                                    } else {
                                        if value === null || (value === "" && (!isset defaultValues[field] || value !== defaultValues[field])) {
                                            let isNull = true;
                                        }
                                    }
                                } else {
                                    if !is_numeric(value) {
                                        let isNull = true;
                                    }
                                }
                            }

Here value hits else condition !is_numeric(value) which is true and set field as null causing this message to being add. Someone would say it's a bug, other would say it's not cuz it's not numeric.



3.4k

var TINYINT(1)

This is your "issue". A TINYINT is storing a representation of (bool) true/false as (int) 1/0. You've set your variable to type (bool).

Try this:

...
public function beforeValidationOnCreate() {
  $this->var = 1; //Now it's (int) 1
}

public function beforeValidationOnUpdate() {
  $this->var = 1; //Now it's (int) 1
}
...