在Yii中返回CActiveDataProvider(return CActiveDataProvi

2019-09-29 15:53发布

我有4个数据库表:

用户 :ID,用户名,密码

楼层 :ID,用户ID,姓名

客房 :ID,楼层,名称

学生 :ID,房间名称,活跃

如何返回CActiveDataProvider为后续SQL命令?

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

但响应:

致命错误:调用一个成员函数getDbCriteria()一个非对象在X:的appserv \ WWW \框架\网络\上线225 CActiveDataProvider.php

Answer 1:

命名占位符是有用的。

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


文章来源: return CActiveDataProvider in Yii
标签: php mysql yii