php - doctrine query WHERE u.status = 1 AND (u.type = 1 OR u.type = 2) -


i'm trying build search form. fill data this:

 private function _get_results() {     $search_data = $this->input->post('type');     if ($search_data) {         $search_data = implode(' or u.type = ', $search_data);         $search_array[] = array(             'key' => 'u.type',             'value' => $search_data,             'operand' => 'eq');     if (count($search_array)) {         $results = $this->accounts_model->get_search_results($search_array);     } 

this model code.

function get_search_results( $params = array(),  $single_result = false,  $order_by = array('order_by' => 'u.id', 'direction' => 'desc') ) {     $qb = $this->doctrine->em->createquerybuilder();     $qb->select('u');     $qb->from($this->_entity, 'u');     $qb->where($qb->expr()->eq('u.status', 1));      foreach ($params $param) {         $qb->andwhere(                 $qb->expr()->$param['operand']($param['key'], $param['value'])         );     }     $qb->orderby($order_by['order_by'], $order_by['direction']);     $qb->setfirstresult(0);     $qb->setmaxresults(20);     echo $qb->getquery()->getdql() . '<br/>';     die;     $result = $qb->getquery()->getresult();     return $result; } 

the line echo $qb->getquery()->getdql() . '<br/>'; returns result:

select u entities\account u u.status = 1 , (u.type = 1 or u.type = 2) order u.id desc 

is there way avoid using implode() same result: ...and (u.type = 1 or u.type = 2)

i'm using codeigniter btw.

this in statement comes in, , querybuilder has support built in can pass in array of values. try this:

$qb->andwhere('u.type in (:types)')->setparameter('types', $search_data); 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -