查询Yii CDbCriteria

时间:2014-08-28 10:02:17

标签: php yii

我需要使用CDbCriteria创建一个查询:

select t_devices.* 
from t_departmens, t_branches, t_cabinets, t_employees, t_devices
where t_branches.id_organization = 1
and t_departmens.id_branch = t_branches.id_branch
and t_cabinets.id_department = t_departmens.id_department
and t_employees.id_cabinet = t_cabinets.id_cabinet
and t_devices.id_employee = t_employees.id_employee

2 个答案:

答案 0 :(得分:0)

试试这个

$criteria = new CDbCriteria;
$criteria->select('t_devices.*'); 
$criteria->from('t_departmens, t_branches, t_cabinets, t_employees, t_devices');
$criteria->condition('t_branches.id_organization = 1
and t_departmens.id_branch = t_branches.id_branch
and t_cabinets.id_department = t_departmens.id_department
and t_employees.id_cabinet = t_cabinets.id_cabinet
and t_devices.id_employee = t_employees.id_employee
');

答案 1 :(得分:0)

建议的方法是在模型文件中使用Active Record(AR)关系。

阅读:Yii's active record guide

将使用Gii

自动生成AR关系

阅读:Yii's Gii Guide