A few challenges where in the Yii Query builder

I want to build a query with several calls where, but I have an error when you use this code

$command = Yii::app()->db->createCommand() ->select('*') ->from('{{table}}'); $command->where('value1 = :value1', array(':value1' => 1)); $command->where('value2 < :value2', array(':value2' => 2)); 

I understand that I can use code like

 $command->where('value1 = :value1 AND value2 = :value2', array(':value1' => 1, ':value2' => 2)); 

but I have complex conditions and its usage code, for example, the top one, is simpler.

In Codeigniter, I can use this condition several times

 $this->db->where() 
+4
source share
2 answers

You need to pass the array like this:

 $command->where(array('AND', 'value1 = :value1', 'value2 < :value2'), array(':value1' => 1, ':value2' => 2)); 
+6
source

I actually found a better way to do this

 $result = array(); $result = Yii::app()->db->createCommand() ->select('*') ->from('table'); $condition = array(); $cond_arg = array(); if (!empty($email)) { $condition[] = "email =:email"; $cond_arg[':email'] = $email; } if (!empty($value2)) { $condition[] = "value2 =:value2"; $cond_arg[':value2'] = $value2; } if (!empty($value3)) { $condition[] = "value3 like :value3"; $cond_arg[':value3'] = $value3; } if (count($condition) > 0) { $result->where(array('AND', implode(" AND ", $condition)), $cond_arg); } return $result->queryAll(); 
0
source

All Articles