复杂选择
除了正常的匹配选择以外,filter
,merge
里还提供了其他复杂选择器。
如果数组中值为数组
的话,会自动变为in
条件语句
// WHERE `user`.`type` IN (1,2,3,'test') $this ->userDAO ->filter (array ('id' =>array (1,2,3,'test' )));
其他还包括 >
,<
,>=
,<=
,!=
,<>
,is
,is not
,同样,多表的情况下需要用二维数组
去封装
// WHERE `user`.`id` >= 10 AND `user`.`time` >= 1461584562 AND `user`.`type` is not null $filter =$this ->userDAO ->filter (array ('>=' =>array ('id' =>10,'time' =>1461584562),'is not' =>array ('type' =>NULL ),- ));
// WHERE `user`.`id` != 3 AND `user`.`id` != 4 AND `user`.`id` != 5 $filter =$this ->userDAO ->filter (array ('!=' =>array ('id' =>array (3, 4, 5))- ));
另外,like语句
也是支持的,可匹配正则符的开始结尾符,具体写法如下:
// WHERE `user`.`name` LIKE '%test%' OR `user`.`type` LIKE 'admin%' OR `user`.`type` LIKE '%admin' $filter =$this ->userDAO ->merge (array ('__like__' =>array ('name' =>'test' ,'type' =>'^admin' ,'type' =>'admin$' ),- ));
// WHERE `user`.`name` LIKE '%test%' OR `user`.`name` LIKE 'admin%' OR `user`.`name` LIKE '%demo' $filter =$this ->userDAO ->merge (array ('__like__' =>array ('name' =>array ('test' ,'^admin' ,'demo$' ),- )
- ));
not in
语法暂时并未支持,可以暂时使用多个!=
或者<>
替代
同时filter/merge
也可以被迭代调用,以应对不确定筛选条件的复杂查询
// 某一个返回筛选数据的Action $DAO =$this ->userDAO ;if ($status =$this ->param ('status' )){$DAO =$DAO ->filter (array ('status' =>$status ));- }
if ($startTime =$this ->param ('start' , 0)){$DAO =$DAO ->filter (array ('>=' =>array ('start' =>$startTime )));- }
if ($endTime =$this ->param ('end' ,time ())){$DAO =$DAO ->filter (array ('<' =>array ('end' =>$endTime )));- }
// 获取复合条件数量 $count =$DAO ->count ();// 获取复合条件前10条数据 $data =$DAO ->limit (10)->query ();