Receiving data using CActiveDataProvider in yii?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
6 views
There are 3 tables, a standard many-to-many.

Users(id,...) -> Users_Has_Courses(Users_id, Courses_id) -> Courses(id,...)


Relationship in the model Courses


'users' => array(self::many_many relations, 'Users', 'users_has_courses(Courses_id, Users_id)')


in the model Users


'courses' => array(self::many_many relations, 'Courses', 'users_has_courses(Users_id, Courses_id)'),


Tell me how to get the list of courses that are not signed by the user with the specified id, i.e. the analogue of the usual request


select * from Courses where id not in (select Courses_id from users_has_courses where Users_id = 2)
by | 6 views

2 Answers

0 like 0 dislike
If the subquery returns many records, then the fastest would be the following:
\r
\r
$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)); 
by
0 like 0 dislike
Thanks, I already did:
\r
\r $criteria = new CDbCriteria();
$criteria->addNotInCondition('id', $coursesIds);
$unsubscribedCourses = new CActiveDataProvider('Courses', array('criteria' => $criteria));
\r
by

Related questions

0 like 0 dislike
3 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Jun 2, 2019 by djienbaev97
0 like 0 dislike
1 answer
asked May 20, 2019 by MasterGerold
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
27,924 users