选择器
DAO类都可以调用filter
(与选择器),merge
(或选择器),效果相当于筛选表内数据
同样选择器支持单表和多表操作,参数中单表为一维数组
,多表则为二维数组
// ... WHERE `user`.`id` = 1 AND `user`.`type` = 'admin' $filter =$this ->userDAO ->filter (array ('id' =>1,'type' =>'admin' ));
而用merge
或选择器筛选,条件则用or
相连接
// ... WHERE `user`.`id` = 1 OR `user`.`type` = 'admin' $merge =$this ->userDAO ->merge (array ('id' =>1,'type' =>'admin' ));
同样多表参数也可用别名
对应表,用法跟上面一致,这里就不展开了
// ... WHERE `user`.`id` = 1 AND `project`.`type` = 'outer' $filter =$this ->userDAO ->join ($this ->projectDAO ,array ('projectId' =>'id' ))- ->
filter (array (array ('id' =>1 ),array ('type' =>'outer' ),- ));
$filter
条件可以继续调用filter
/merge
方法,条件会在原来的基础上继续筛选
// ... WHERE (...) OR (`user`.`name` = 'test') $filter =$filter ->merge (array ('name' =>'test' );
$filter
条件也可以作为参数传入filter
/merge
方法。效果为条件的叠加。
// ... WHERE (`user`.`id` = 1 AND `user`.`type` = 'admin') OR (`user`.`id` = 2 AND `user`.`type` = 'user') $filter1 =$this ->userDAO ->filter (array ('id' =>1,'type' =>'admin' );$filter2 =$this ->userDAO ->filter (array ('id' =>2,'type' =>'user' ));$merge =$filter1 ->merge ($filter2 );
无论是与选择器
还是或选择器
,条件本身作为参数时,条件自身的DAO
必须和被选择对象的DAO
保持一致,否者会抛出异常
值得注意的是filter
和merge
的先后顺序对条件筛选是有影响的
可以参考下面这个例子
// WHERE (`user`.`id`=1 AND `user`.`type`='admin') OR `user`.`id`=2 $this ->userDAO ->filter (array ('id' =>1,'type' =>'admin' )->merge (array ('id' =>2));// WHERE `user`.`id`=2 AND (`user`.`id`=1 AND `user`.`type`='admin') $this ->userDAO ->merge (array ('id' =>2))->filter (array ('id' =>1,'type' =>'admin' );
由上述例子可知,添加之间关联符是跟后面
的选择器表达式保持一致
选择器
获取数据跟DAO
方法一致,单表的选择器
具有单表的所有查询,删改方法,而多表的选择器
具有多表的所有查询,修改方法
// UPDATE `DATABASE`.`TABLE` AS `user` SET `user`.`name` = 'test' WHERE `user`.`id` = 1 $result =$this ->userDAO ->filter (array ('id' =>1)->update (array ('name' =>'test' ));// SELECT * FROM ... WHERE `project`.`type` = 'admin' $result =$this ->userDAO ->join ($this ->projectDAO ,array ('projectId' =>'id' ))- ->
filter (array (array (),array ('type' =>'admin' )))- ->
query ();
另外,如果想实现where start=end
或者where start=end+86400
这类的条件也是支持的,方法如下:
// ... WHERE `user`.`lastLoginTime` = `user`.`registerTime` and `user`.`lastLoginTime` <= refreshTime+86400 $filter =$this ->userDAO ->filter (array ('lastLoginTime' =>Database::field ('`user`.`registerTime`' ),'<=' =>array ('lastLoginTime' =>Database::field ('refreshTime+86400' )),- ));
无论是filter
还是merge
,在执行SQL语句前都不会被执行
,不会增加sql负担,可以放心使用。