We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Boolean properties in model when using PHP 7.4

Hello, as now we have typed properties in PHP 7.4 what is best practice how to deal with boolean values in the Phalcon 4 models (DB behind is MySQL)? Problem that DB keeps them as shortint(1) but I can't keep properties boolean as I need convert type and set them back in afterFetch/beforeSave so I still use

/*@var $bar int|bool */
public $bar;

instead of

public bool $bar;

This is a disconnect between what you want (boolean) and what the database uses (tinyint). Basically, MySQL doesn't use boolean values so you can't type the property as bool. I don't believe this is a problem unique to Phalcon.

If you absolutely need to enforce that $bar is always boolean, then I believe you can override \Phalcon\Mvc\Model::__get() and \Phalcon\Mvc\Model::__set to throw exceptions when an attempt is made to set $bar to something non-boolean, in addition to afterFetch() and beforeSave()