Coder Social home page Coder Social logo

jsonize's Introduction

jsonize

This extension is a simple to use function for json encoding CActiveRecord and their relations

##Requirements

  • PHP 5.2 or above
  • Yii 1.1 or above

##Installation

  • copy jsonize.php into your protected/extensions/jsonize folder
  • add the following line to your config/main.php:
'import' => array(
    ...
    'ext.jsonize.Jsonize',
    ...
),

##Usage

$users = User::model()->with('votes')->findAll($criteria);

echo Jsonize::encode($users); // all attributes and loaded relations ('votes' in this example) are processed
$array = Jsonize::encode(($users); // same as before, but returns an array of attributes/relations, not a JSON-encoded string

echo Jsonize::encode($users, array('posts','comments')); // all attributes are processed, specified relations and loaded ones ('posts','comments' and 'votes') are processed

echo Jsonize::encode($users, array('posts','comments'), true); // all attributes are processed, but only specified relations ('posts','comments') are processed

echo Jsonize::encode($users, array('id', 'username', 'email', 'first_name', 'last_name', 
    'posts' => array( 'id', 'author_id', 'content' ),
    'comments' => array( 'id', 'author_id', 'content' ),
    'votes' => array( /* attributes here */))
), true, true); // only specified attributes and relations are processed

#Processing nested relations

By default, all attributes (returned by getAttributes()) and all first-level LOADED relations are processed. Nested relations have to be loaded explicitly:

echo Jsonize::encode($users, array('posts'=>array('comments'))); 
// output {
	"id":"1",
	"name":"David",
	"posts":[
		{
			"id":"1",
			"user_id":"1",
			"title":"new post",
			"comments": [{
				"id": 1,
				"post_id": "1"
				"text": "Nice post"
			}]
		},{
			"id":"2",
			"user_id":"1",
			"title":"other post",
			"comments": [{
				"id": "2",
				"post_id": "2",
				"text": "comment other post"
			},{
				"id": "3",
				"post_id: "2",
				"text": "other comment other post"
			}]
		}
	]
}

There's no limit to this notation, you could do:

echo Jsonize::encode($users, array('posts'=>array('user'=>array('posts','comments'))));
// it will output also $user->posts, $user->posts->user, $user->posts->user->posts and $user->posts->user->comments

##Notes

For nested relations, the policy specifying which attributes/relations are to be processed is the same as the first one. For example:

echo Jsonize::encode($users, array('id','posts'=>array('title','user'=>array('name')), true, true)); 
// only specified attributes and relations of user, user.posts and user.posts.user will be processed

##API

/**
 * @param mixed $data an ActiveRecord or array of ActiveRecord
 * @param $attributes array of attributes/relations to be processed e.g ['client','items' => ['product']] // nested relations
 * @param bool $onlySpecifiedRelations if to send all loaded relations or only the ones specified in $attributes 
 * @param bool $onlySpecifiedAttributes if to send all attributes or only the ones specified in $attributes 
 */
function jsonize($data, $attributes=true, $onlySpecifiedRelations = false, $onlySpecifiedAttributes = false);

/**
 * shortcut for json_encode(jsonize($data))
 * @see jsonize()
 */ 
function jsonizenc($data, $attributes=true, $onlySpecifiedRelations = false, $onlySpecifiedAttributes = false);
}

jsonize's People

Contributors

cnlevy avatar lennartvdd avatar stefanwuest avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.