Yii 1.1: carjson

Convert model and its relations to a JSON String
8 followers

To develop this extension I started from ejsonbehavior. That extension wasn't fitting my own requirements so I just developed a new and fresh one!

Requirements

Was developed using Yii 1.1.4

Install

Extracxt the json directory to your protected/extensions/ directory

Usage

The first parameter can be a single model or an array of models. The other two parameters has to be array.

echo $json->toJSON( mixed $model, array $relations, array $attributes);
Yii::import('application.extensions.json.CArJSON');
$users = User::model()->findAll($criteria);
 
$json = new CArJSON();
// Define which relations do you want to load
$relations = array('posts', 'comments', 'votes');
// Define which attributes, foreach relations do you want to load
// root denote attributes for the main model
$attributes = array(
    'root' => array('id', 'username', 'email', 'first_name', 'last_name'),
        'posts' => array( 'id', 'author_id', 'content' ),
    'comments' => array( 'id', 'author_id', 'content' ),
    'votes' => null, // This will load all attributes
);
echo $json->toJSON($users, $relations, $attributes);

Resources

Total 5 comments

#15359 report it
kfahmi at 2013/10/31 11:45pm
JQUery parse

Extension Work nicely.. the output (JSON) comes up like this :

[
{"attributes":{"id_tp":"199","id_posttag":"164","id_usertag":"29","id_group":null,"new":"1"},"relations":{"idPost":{"id_post":"164","notes":"asdsad"}}},
]

But...I have a problem when im using JQuery to parse this JSON (output).

that output shows up like this :

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

is there anybody can help me??how using the jquery code to get this JSON and parse it?

this is my code that use for parse the JSON

$(document).ready(function(){
var session = window.localStorage.getItem('example');
var obj = jQuery.parseJSON(session);
var idnya = obj.id_user;                                                            $.get("http://192.168.12.3/leapdeck.com/android/posting/tagpost",
{                                                                               idUser:idnya
},
                                                                function(data,status)
{                                                                               
 
var tagpost = data;                                                                                var tag = jQuery.parseJSON(tagpost);                                                                                            alert(tag);
});
});

Thanks for helping me anyway :)

#13131 report it
G_Gus at 2013/05/07 04:26am
Alternative output style

In order to have an object that more closely resembles the Active Record, you can change the $jsonDataSource = array('attributes'=>$attributes,'relations'=>$this->relations); line in the getJSONModel to $jsonDataSource = CMap::mergeArray($attributes, $this->relations);

#7108 report it
bonnie at 2012/02/23 09:51pm
Thanks StErMi

Hi, StErMi I want a way to pass the data of the model from json to javascript where I have list list with the item_name from the item model. The same as your example but wants to pass the json to javascript. Thanks.

#2352 report it
Maurizio Domba Cerin at 2010/12/16 05:43am
note

The link for the project page is wrong - http://www.yiiframework/

Leave a comment

Please to leave your comment.

Create extension