Reputation: 175
Three tables Project, Users, Issues.
Relations:
What I want to do:
In Yii framework logic: Select Project with all it's users, these users has to have only Issues of the selected Project.
In tables logic: Select Issues of certain project AND user.
What sql code I want to mimic:
SELECT Issue.i_name FROM Issue Join Project on Issue.i_id = Project.p_id Join User on Issue.i_id User.u_id
What I want to do in Yii:
//get Project
$model = Project::model()->findByPk( $p_id );
//get Project's users
$users = $model->users;
//get each of users issues of selected project
foreach( $users as $user )
$issues = $user->issues;
Upvotes: 3
Views: 385
Reputation: 301
To solve this you have to use through in your ralations method.
Project model relations method should look like this:
public function relations()
{
return array(
'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
//'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'issues' => array(self::HAS_MANY,'Issue',array('id'=>'owner_id'),'through'=>'users'),
'columns' => array(self::MANY_MANY, 'Column', 'tbl_project_rel_column(p_id,c_id)'),
);
}
Now in action select project, it's users and users's posts(or in my case issues) of selected project:
$project = Project::model()->with('users','issues')->findByPk(1);
$users = $project->users;
foreach($users as $user) {
echo $user->username."<br/>";
}
$issues = $project->issues;
foreach($issues as $issue) {
echo $issue->name."<br/>";
}
Upvotes: 2