carjson Convert model and its relations to a JSON String

  1. Requirements
  2. Install
  3. Usage
  4. Resources

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

6 0
9 followers
964 downloads
Yii Version: 1.1
License: GPL-3.0
Category: Others
Tags: JSON
Developed by: StErMi
Created on: Dec 14, 2010
Last updated: 13 years ago

Downloads

show all

Related Extensions