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.

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



53.5k
Accepted
answer

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



4.4k

I had the same question some weeks ago.

My solution was finally this code: http://forum.phalconphp.com/discussion/3493/saving-boolean-value-in-mysql#C11534