Question

How I can return object with all relations(ans sub objects relations?). Now I use EJsonBehavior but it returns only first level relations, not sub related objects. My source code:

    $order = Order::model()->findByPk($_GET['id']);
    echo $order->toJSON();
    Yii::app()->end();
Was it helpful?

Solution

The eager loading approach retrieves the related AR instances together with the main AR instance(s). This is accomplished by using the with() method together with one of the find or findAll methods in AR. For example,

$posts=Post::model()->with('author')->findAll();

The above code will return an array of Post instances. Unlike the lazy approach, the author property in each Post instance is already populated with the related User instance before we access the property. Instead of executing a join query for each post, the eager loading approach brings back all posts together with their authors in a single join query!

We can specify multiple relationship names in the with() method and the eager loading approach will bring them back all in one shot. For example, the following code will bring back posts together with their authors and categories:

$posts=Post::model()->with('author','categories')->findAll();

We can also do nested eager loading. Instead of a list of relationship names, we pass in a hierarchical representation of relationship names to the with() method, like the following,

$posts=Post::model()->with(
    'author.profile',
    'author.posts',
    'categories')->findAll();

The above example will bring back all posts together with their author and categories. It will also bring back each author's profile and posts.

Eager loading may also be executed by specifying the CDbCriteria::with property, like the following:

$criteria=new CDbCriteria;
$criteria->with=array(
    'author.profile',
    'author.posts',
    'categories',
);
$posts=Post::model()->findAll($criteria);

or

$posts=Post::model()->findAll(array(
    'with'=>array(
        'author.profile',
        'author.posts',
        'categories',
    )
);

OTHER TIPS

I found the solution for that. you can use $row->attributes to create data

    $magazines = Magazines::model()->with('articles')->findAll();


    $arr = array();
    $i = 0;
    foreach($magazines as $mag)
    {   
        $arr[$i] = $mag->attributes;
        $arr[$i]['articles']=array();
        $j=0;
        foreach($mag->articles as $article){
            $arr[$i]['articles'][$j]=$article->attributes;
            $j++;
        }
        $i++;
    }
    print CJSON::encode(array(
            'code' => 1001,
            'magazines' => $arr,
        ));

This is the best piece of code I found after a long time search to meet this requirement. This will work like Charm.

 protected function renderJson($o) {
    //header('Content-type: application/json');
    // if it's an array, call getAttributesDeep for each record
    if (is_array($o)) {
        $data = array();
        foreach ($o as $record) {
            array_push($data, $this->getAttributes($record));
        }
        echo CJSON::encode($data);
    } else {
        // otherwise just do it on the passed-in object
        echo CJSON::encode($this->getAttributes($o));
    }

    // this just prevents any other Yii code from being output
    foreach (Yii::app()->log->routes as $route) {
        if ($route instanceof CWebLogRoute) {
            $route->enabled = false; // disable any weblogroutes
        }
    }
    Yii::app()->end();
}

protected function getAttributes($o) {
    // get the attributes and relations
    $data = $o->attributes;
    $relations = $o->relations();
    foreach (array_keys($relations) as $r) {
        // for each relation, if it has the data and it isn't nul/
        if ($o->hasRelated($r) && $o->getRelated($r) != null) {
            // add this to the attributes structure, recursively calling
            // this function to get any of the child's relations
            $data[$r] = $this->getAttributes($o->getRelated($r));
        }
    }
    return $data;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top