Answer the question
In order to leave comments, you need to log in
Getting data with CActiveDataProvider in yii?
There are 3 tables, a standard many-to-many relationship.
Users(id,...) -> Users_Has_Courses(Users_id, Courses_id) -> Courses(id,...)
Relationship in Courses model
'users' => array(self::MANY_MANY, 'Users', 'users_has_courses(Courses_id , Users_id)')
in the model Users
'courses' => array(self::MANY_MANY, 'Courses', 'users_has_courses(Users_id, Courses_id)'), Tell me
how to get a list of courses that the user with the specified id is not subscribed to, those. analogue of the usual query
select * from Courses where id not in (select Courses_id from users_has_courses where Users_id = 2)
Answer the question
In order to leave comments, you need to log in
If the subquery returns many records, then the following will be the fastest:
$courses = Courses::model()->findAllBySql(
"SELECT * FROM Courses WHERE id NOT IN (SELECT courses_id FROM users_has_courses WHERE users_id=:u)",
array(':u'=>2));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question