我有以下查询:
select
`t`.`id` AS `id`,
`rm`.`role_id` AS `role_id`,
`t`.`id` AS `sequence`,
`t`.`parent_id` AS `parent_id`,
`t`.`label` AS `label`,
`t`.`order` AS `order`,
(case
when isnull(`rm`.`id`) then 0
else 1
end) AS `description`,
`tb`.`label` AS `parent_label`
from
((`tbl_menu` `t`
left join `tbl_menu` `tb` ON ((`t`.`parent_id` = `tb`.`id`)))
left join `tbl_role_menu` `rm` ON ((`rm`.`menu_id` = `t`.`id`))) and rm.role_id = $role_id
where
isnull(`tb`.`label`)
union select
`t`.`id` AS `id`,
`rm`.`role_id` AS `role_id`,
`t`.`parent_id` AS `parent_id`,
`t`.`parent_id` AS `sequence`,
`t`.`label` AS `label`,
`t`.`order` AS `order`,
(case
when isnull(`rm`.`id`) then 0
else 1
end) AS `description`,
`tb`.`label` AS `parent_label`
from
((`tbl_menu` `t`
left join `tbl_menu` `tb` ON ((`t`.`parent_id` = `tb`.`id`)))
left join `tbl_role_menu` `rm` ON ((`rm`.`menu_id` = `t`.`id`))) and rm.role_id = $role_id
where
(`tb`.`label` is not null)
order by `sequence` , `parent_id` , `label`
在两个查询中,在第二个左边的连接中,我必须传递一个变量$ role_id。目前,我对视图有这个查询,但如果尝试传递条件条件,则生成的查询为
select * form menu_links where role_id = $role_id
正在menu_links视图的名称。这不能给我我想要的结果。我需要一种方法将此参数添加到此查询并将其转换为CDbCriteria,以便将其传递给CGridView。有什么帮助吗?
感谢。
答案 0 :(得分:2)
考虑使用CArrayDataProvider。
CArrayDataProvider充当简单关联数组的包装器,CGridView不会知道它们的区别。您甚至可以应用分页,排序等。可以在文档中找到展示这些功能的示例:
$rawData=Yii::app()->db->createCommand('SELECT * FROM tbl_user')->queryAll();
$dataProvider=new CArrayDataProvider($rawData, array(
'id'=>'user',
'sort'=>array(
'attributes'=>array(
'id', 'username', 'email',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
答案 1 :(得分:1)
我举一个简单的例子,你将弄清楚如何将它应用到你的案例
$sql= "select * form menu_links where role_id = :role_id";
$role_id='Something you will get from your could';
$command = Yii::app()->db->createCommand($sql);
// And finally the command you can replace the role id with varibale is
$command->bindParam(":role_id", $role_id, PDO::PARAM_STR);
$result = $command->queryAll();
我希望这就是你所要求的。