Return CActiveDataProvider in Yii
I have 4 database tables:
User : id, username, password
Floor : id, userid, name
Room : id, floor, name
Student : id, number, name, active
How to return CActiveDataProvider for next SQL command?
SELECT * FROM `student` WHERE `student`.`active` = 0 AND `student`.`room` IN (SELECT `room`.`id` FROM `room` WHERE `room`.`floor` = (SELECT `floor`.`id` FROM `floor` WHERE `floor`.`userid` = $userid))
I tried:
$model = Student::model()->findAllBySql("SELECT * FROM student WHERE active = 0 AND room IN (SELECT id FROM room WHERE floor = (SELECT id FROM floor WHERE user = $user))");
return new CActiveDataProvider($model);
But the answer is:
Fatal error: Call member function getDbCriteria () for non-object in X: appserv \ www \ framework \ web \ CActiveDataProvider.php on line 225
+1
source to share
1 answer
The named placeholder is useful.
$criteria = new CDbCriteria;
$criteria->condition = 'student.active = 0 AND student.room IN (SELECT room.id FROM room WHERE room.floor = (SELECT floor.id FROM floor WHERE floor.userid = :userid))';
$criteria->params=array(':userid'=>$userid);
$dataProvider = new CActiveDataProvider('Student',array('criteria'=>$criteria,));
0
source to share