unframed / JSONModel.php

"People love ORMs"
GNU Lesser General Public License v3.0
28 stars 1 forks source link

JSON column explanation #2

Closed samlevin closed 9 years ago

samlevin commented 9 years ago

Can I use JSOModel without the JSONColumn? what is the purpose of the JSON column? In order to keep my db schema pure, is a good solution to add the JSON column and then repair the db (in say, an installation script)? This seems like excellent software, just a bit confused about some of the workings! Thanks!

laurentszyster commented 9 years ago

Thanks for the appreciation.

Can I use JSOModel without the JSONColumn ?

Yes, you can use JSONModel to access SQL tables and views without json_column.

What is the purpose of the JSON column ?

The purpose of adding a json_column is to hold non-scalar values (ie: JSON lists and objects). It is also an opportunity to cache JSON encoded, eventually consistent, representation of each relation. In effect it is a rather cheap object store ,-)

Eventually, all SQL databases have at least one entity table with one or more columns holding non-scalar value. The option_value column of WordPress can hold non-scalar option values, for instance. Legacy PHP applications use the language own serialization and new ones tend to prefer JSON's interroperability, but they all at some point need to store non-scalar values in an SQL column.

So, instead of adding more non-scalar columns, why not have one to leverage consistently ?

In the insert and replace method, that json_column is used to cache a JSON encoded representation of the whole relation with all the scalar columns. In the select method (if available) it's content is decoded and merged with the results set, dispensing applications of a tedious chore. And the json method will select and return a list of JSON strings as fast as possible (ie: without decoding nor encoding).

In order to keep my db schema pure, is a good solution to add the JSON column and then repair the db (in say, an installation script) ?

JSONModel::repair can be used to extend a legacy database in an installation script.

It can also be used to create a brand new database.