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.

Phalcon models support for JSON fields

How is the Phalcon ORM support for JSON fields? How model properties map to JSON fields in the database? Are they simply treated as strings? I'm designing a database and have some JSON fields to simplify the design. Would I need to manually decode thoses JSON data fields? What about relationships? Anyone in a similar position? Ideas and examples are very welcomed. Thanks!



33.6k
Accepted
answer

They are treated as strings, but you can use afterFetch and beforeSave to transform properly for usage in the PHP userland.



44.8k

So then before a record is saved it turns that field into JSON in beforeSave. Then I suppose that if I want to be able to use the record normally after a save that I would need to convert it back to a PHP array in afterSave. I think that it would be much more powerful if there was either a static method way to do this that didn't need to mutate the data attached to the model or also a saveTransform (something like that) method. I mean we don't need 7 classes strung together like Java but it would be really nice to be able to save the data in a particular way without modifying the perfectly well adjusted model.