njson

Extends the capabilities the CJSON to traverse ActiveRecord objects relations
9 followers

While working on a project we faced the need to sync data between the browser and the server in JSON format, most of these data are ActiveRecord(s), after some search we found "ejsonbehavior" ... it is really a cool extension but unfortunately the output was not as we expected, so we decided to extend the capabilities of the CJSON to allow traversing ARs relations

Requirements

Developed and tested under Yii 1.1.13

Usage

Simply

NJSON::encode(SomeModel::model()->with('relation', 'relation.deep_relation')->findAll());

Resources

Total 6 comments

#16353 report it
cnlevy at 2014/02/13 05:21pm
Re: Re: Filter

Try jsonize
Filtering using CArJSON usage example:

Yii::app()->jsonize;
$users = User::model()->with('votes')->findAll($criteria);
echo jsonizenc($users); // all attributes and loaded relations ('votes' in this example) are processed
echo jsonizenc($users, array('posts','comments')); // all attributes are processed, specified relations and loaded ones ('posts','comments' and 'votes') are processed
echo jsonizenc($users, array('posts','comments'), true); // all attributes are processed, but only specified relations ('posts','comments') are processed
echo jsonizenc($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
#15779 report it
Amr Bedair at 2013/12/16 01:19pm
Re: Filter

Actually not at this moment but I can add it ... hopefully soon

#15776 report it
lucianocn at 2013/12/16 12:20pm
Filter

First of all, thanks! I really liked your extension! I was using CArJSON (http://www.yiiframework.com/extension/carjson/) and it has an option to filter model/relations columns. Is it possible to do something like it?

Thanks

#15141 report it
luckas at 2013/10/10 08:23pm
Re: Re: Key with numeric name

Hi Amr, we are storing an array with numerical keys into an attibute, we use your extension to serialize this model and and sending trough ajax

this is an example:

  • Model
    • Attribute = array("1"=>"34.33","2"=>"22.65");
#15140 report it
Amr Bedair at 2013/10/10 06:30pm
Re: Key with numeric name

Thanks for trusting my code ...
- nameValue is just a copy of the original CJSON's one
- what is the case that the object's property name allowed/can be numerical!?

#15139 report it
luckas at 2013/10/10 04:26pm
Key with numeric name

Hi, we found a little bug in the function "nameValue" when you serialize in key=>values an array, if the key is an numeric value the extensión serialize like a number and create an invalid JSON, for now we solved this using this:

protected static function nameValue($name, $value)
{
    //return self::encode(strval($name)) . ':' . self::encode($value);
 
    // The Keys must be enclose with quotes
    return '"'.strval($name).'" :' . self::encode($value);
}

You made a Great work ;-)

Cheers from Argentina

Leave a comment

Please to leave your comment.

Create extension
Downloads