The best part of a MVC architecture is its simple way to fetch data from the database with model relationship . Today i am going to discuss How to fetch data from multiple dependent tables. Suppose we have three tables .
1) user table 2) user posts table with user id 3) postdata with post id
so if we want to get all the users with posts and post content we can simply define there relation in the model . like given below
User.php

class User extends CActiveRecord
{
/// other stuff
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'posts' => array(self::HAS_MANY, 'Posts', 'user_id'),
);
}
}

Posts.php

class Posts extends CActiveRecord
{
/// other stuff
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'postsdata' => array(self::HAS_MANY, 'PostData', 'post_id'),
);
}
}

no we have a relation and with just one query we can fetch the data with all content . like

$userposts=User::model()->with(array('posts','posts.postsdata'))->FindAll();

now the userposts variable will contain all the posts with their post data.

http://codingbin.com/wp-content/uploads/2015/11/yii.jpghttp://codingbin.com/wp-content/uploads/2015/11/yii-150x150.jpgdevidframeworksYiiThe best part of a MVC architecture is its simple way to fetch data from the database with model relationship . Today i am going to discuss How to fetch data from multiple dependent tables. Suppose we have three tables . 1) user table 2) user posts table with user...coding, php, wordpress, yii, cakephp, and  jquery

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request