php - How to express OR conditions with the CakePHP 3.x query builder? -
i trying build query in cakephp 3.x following structure : , b , c , [d or e] a,b, c, d, e contains array of values joined or conditions
$schools = $schools->where(['a in' => $arrayofaoptions]), $schools = $schools->where(['b in' => $arrayofboptions]), $schools = $schools->where(['c in' => $arrayofcoptions]) // want firstly joined or before joining other conditions , $schools = $schools->where(['d in' => $arrayofcoptions]) //or $schools = $schools->where(['e in' => $arrayofcoptions])
the problem here , not fields d , e isolated or , join others and
in cakephp 2, managed achieve following codes
$queryconditions = array( 'or'=> array_merge( array( 'school.d' => $this->session->read('conditions.d') ), array( 'school.e' => $this->session->read('conditions.e') ) ) );
before merging rest on conditions appreciated
you can use same style in cakephp 2.x, ie pass conditions nested under or
key. can wrap in single where()
call.
$schools->where([ 'a in' => $arrayofaoptions, 'b in' => $arrayofboptions, 'c in' => $arrayofcoptions, 'or' => [ 'd in' => $arrayofdoptions, 'e in' => $arrayofeoptions ] ]);
you use orwhere()
, it has been deprecated lately behavior kinda fragile.
finally, can use expressions create complex conditions. or_()
expression method can used create or
conditions:
$schools->where(function ($expr) use ($arrayofaoptions, $arrayofboptions) { return $expr->or_([ 'a in' => $arrayofaoptions, 'b in' => $arrayofboptions, ]); });
see cookbook > database access & orm > query builder > advanced conditions
Comments
Post a Comment