Reputation: 32243
I'm starting with yii.
I have the following DB structure:
Table: Rows:
user [id,login,password,name,email]
userToProject [user_id,project_id,role]
project [id,name,status]
And I want to retrieve all the users working in a project with status=3 as role=manager. And this is my code by the way, I need to make the second join to reach the project status.
$criteria=new CDbCriteria;
$criteria->join='INNER JOIN {{userToProject}} a ON t.id=a.user_id and a.role='.Role::MANAGER;
$criteria->distinct=true;
return User::model()->findAll($criteria);
Can I make it with a criteria or should I implement an SQLcommand and run it?
Thanks
Upvotes: 1
Views: 2710
Reputation: 549
well you make relation in userToProject model like this...
'user'=>array(self::BELONGS_TO, 'User', 'user_id')
// after belongs_to User is a model class name..
'project'=>array(self::BELONGS_TO, 'Project', 'project_id'),
// after belongs_to Project is a model class name..
and then use active record like ..
$allrecord = UserToProject::model()->with('user','project')->findAll(
'status = :status AND role = :role',
array(':status' => 3 , 'role' => 'manager'));
//in with bracket user , project is your relation name..
now in $allrecord you have all the record in array form...
Upvotes: 4