本文介绍了在Yii中返回CActiveDataProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有4个数据库表:
用户:ID,用户名,密码
User: id, username, password
楼层:ID,用户ID,名称
Floor: id, userid, name
房间:ID,楼层,名称
学生:身份证,房间,姓名,现役
Student: id, room, name, active
如何为以下SQL命令返回CActiveDataProvider?
How to return CActiveDataProvider for the follow 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))
我尝试过:
$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);
但是回应:
推荐答案
命名占位符很有用.
$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,));
这篇关于在Yii中返回CActiveDataProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!