One fellow programmer at the forum was asking how to convert the ActiveRecord model and its relations to a JSON String. He said that other frameworks do that and did some research and found nothing on this subject.
This is why I created this small behavior, to do exactly that.
Was developed using Yii 1.1.4.
Download and unzip its contents to your application's protected/behaviors folder -the zipped package has the behaviors folder within in case you haven't done that.
// // Once EJsonBehavior is placed in the folder // We need to configure the behavior() function // in your model public function behaviors() { return array( 'EJsonBehavior'=>array( 'class'=>'application.behaviors.EJsonBehavior' ), ); }
Then, to convert your AR object to a JSON string is, as said, 'like in other frameworks', one line of code:
echo $model->toJSON();
I think this could be a good starting point to create not only Yii extensions on the server side but also on the client side in form of jQuery plugins that take JSON Yii Objects as part of their configuration steps.
Just like ExtJS javascript library does with all of its objects.
Total 7 comments
I had to update this to limit the relations if you pass the $rels argument to the toJSON method.
and you can now call it like so:
If the author wants to roll this into an update, it will be backwards compatible and should prevent some requests from hanging. I imagine this will be a common problem in production sites.
Well done guys!
I would like the Yii development team to amend their core CJSON::encode() function to function like this. Also, Bruno's method is more feasible and easy to parse.
Thank you guys.
It seems to go only one relation deep :(
-edit- nevermind I created my own one http://www.yiiframework.com/extension/morray/
thanks for getting me started :)
I just need this, thanks a lot!
Your approach is very good brunotavares.
I've done it with 'jsonDataSource' and 'relations' objets, so to create a naming convention (sort of) just in case there are fellow programmers who wish to create jQuery client plugins with this. As by just including its properties would be much harder than knowing in advance that has a 'jsonDataSource' and 'relations' objects within.
It could be cool to push data in JSON format from the server and CGridView to be totally updated by an AJAX call (imagine this JSON object with other CGridView properties -i.e. columns, headers, and CGridView to be able to update its view)
Nevertheless, great contribution!
I like this!
nice work
Thank you very much for this behavior! I have an addition on how the JSON is generated. I have 2 models, Sale and SaleProducts. Doing $sale->toJSON(); the result is:
So I changed your behaviour to this:
And now the result is:
I don't have jsonDataSource, attributes, or relations anymore. I have a plain JSON with attributes and the relations named.
I guess is better doing json.id than json.jsonDataSource.attributes.id, or json.products than json.relations.products.
Cheers!
Leave a comment
Please login to leave your comment.