phalcon查询技巧

原创
2016-07-29 09:13:05 985浏览

简要介绍几个使用phalcon查询的小技巧

1.使用find,和in来查询,如下:

$orderIdList = array_unique(array_map('intval',$orderIdList));
if ($orderIdList) {
			$orderList = ChildOrder::find([
				'conditions'=>'parents_id IN ({orderIdList:array})',
				'bind'=>['orderIdList'=>$orderIdList]
			]);
		}
这里的$orderIdList是一个数组,使用这种查询方式就查询出类似这样的效果
select * from `childorder` where parents_id in ($orderIdList);

2.巧用model来批量更新数据

上面已经查询出对象$orderList,接下来要批量修改对象中的某一列的值,可以这样做

		foreach($orderList as $row){
			$row->state = 0;
			if ($row->save() == false) {
				foreach ($orderList->getMessages() as $message) {
					throw new \Exception('更新失败');
				}
			}
		}
这样做效果类似于
update `childorder` set state = 0 where parents_id in ($orderIdList);

以上就介绍了phalcon查询技巧,包括了Exception方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。