Hi,
I’m a beginner here so am trying to get my head around a couple of issues.
I have a database set-up with let’s say 3 tables.
user (id, name, status)
user_skill (id, user_id, skill_id)
skill (id, name)
As you can I’m sure guess, a user can have many skills.
Now, I have set-up the User model with the following relations:
public function relations()
{
return array(
'skills' => array(self::HAS_MANY, 'UserSkill', 'user_id'),
);
}
In a controller I am getting a list of all of the users with the following code:
$criteria = array(
'order'=>'id DESC',
'condition'=>'status=1',
);
$dataProvider = new CActiveDataProvider('User', array('criteria'=>$criteria,));
Which works fine.
What I want to now do is get all the users who have specific skills. So say I have a list of skills (1,2,3)
If I was going to do this in SQL I would do something like:
SELECT *
FROM user u
WHERE EXISTS(
SELECT * FROM user_skill s
WHERE s.skill_id in (1,2,3)
AND u.id = s.user_id
)
There’s probably a better way of doing that, but you get the drift.
How would I do that using Yii? Can I add an extra bit to the criteria?
I’ve been tearing my hair out trying to work this out! Any help would be gratefully appreciated.