如何在fuelPHP中使用orm执行此查询:
SELECT * FROM user when a=1 and b=2 and (c=1 or d=2 or e=3)
我不知道如何实现此部分and (c=1 or d=2 or e=3)
坚持:
$where = array()
$where[] = array('a'=>1,'b'=>2);
//Adding c,d,e column in $where[]
I dont know how to combine AND and OR
$query = Model_User::find('all', array(
'where' => $where
));)
有没有办法实现这个目标?
先谢谢
答案 0 :(得分:0)
您可以像这样使用where_or:
$q = Model_User::query();
$q->where('a', '=', 1);
$q->and_where('b', '=', 2);
$q->or_where_open();
$q->where('c', '=', 1);
$q->or_where('d', '=', 2);
$q->or_where('e', '=', 3);
$q->or_where_close();
或者确实喜欢这个(我在选择时发现更容易阅读:
{{1}}
答案 1 :(得分:0)
您也可以这种方式使用它,或者查看此链接以获得更多光线{Query builder Where - Classes FuelPHP documentation
//this should be done within the model that you wish to deal with
$query = DB::select("*");
$query->from->("user");
$query->where("a","=","1");
$query->and_where("b","=","2");
$query->where(function($query){
$query->where("c","=","1");
$query->or_where("d","=","2");
$query->or_where("e","=","3");
});
$query->execute();
答案 2 :(得分:0)
你可以链,所以这更具可读性:
$q = Model_User::query()
->where('a', '=', 1)
->and_where('b', '=', 2)
->or_where_open()
->where('c', '=', 1)
->or_where('d', '=', 2)
->or_where('e', '=', 3)
->or_where_close();
如果要使用数据库查询来提高性能,但仍需要ORM模型对象,可以使用as_object(),请参阅http://fuelphp.com/docs/packages/orm/crud.html#/custom_sql