S
S
Sirix2010-12-06 17:21:32
Yii
Sirix, 2010-12-06 17:21:32

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

2 answer(s)
C
calg0n, 2010-12-07
@calg0n

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));

S
Sirix, 2010-12-07
@Sirix

Thanks, I already did something like this:
$criteria = new CDbCriteria();
$criteria->addNotInCondition('id', $coursesIds);
$unsubscribedCourses = new CActiveDataProvider('Courses', array('criteria' => $criteria));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question