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

Is there a way to do type mapping between database and model

Hello everybody,

As in the post title: Is there a way to easily map PHP types to database types (MySQL in my case)?

I have a table with a lot of columns TINYINT(1) which hold 0 / 1 values. In my model I wouild like them to be mapped to TRUE / FALSE on PHP side but when saving to database they should be converted back to 0 / 1.

You should use property getters and setters for this.

But then I would also have to create my own toArray method and probably couple more... It would be best to have ability like that provided by framework somehow



125.8k
Accepted
answer

In the afterFetch() and beforeSave() methods of the model, you can convert from ints to boolean & vice versa.



6.6k

I had the same question some weeks ago.

My solution was finally this code: https://forum.phalcon.io/discussion/3493/saving-boolean-value-in-mysql#C11534