ejsonbehavior Converts Model Attributes and its Relations to a JSON string

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

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.

Requirements

Was developed using Yii 1.1.4.

Usage

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();

Thoughts

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.

Resources

14 0
19 followers
2 137 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Others
Tags: behavior, JSON
Developed by: Antonio Ramirez
Created on: Nov 6, 2010
Last updated: 14 years ago

Downloads

show all

Related Extensions