[ACCEPTED]-How to create WHERE IN clause with Zend_Db_Select-zend-db

Accepted answer
Score: 178

you can also use it like this:

$data = array(1,3,4);
$select->where('status_id IN(?)', $data);

you dont need 1 to implode array, and it's safer

Score: 12

The first answer probably works in ZF1 but 5 it doesn't work in Zend Framework 2:

$data = array(1,3,4);
$select->where('status_id IN(?)', $data);

In case 4 the Zend Framework2 I found out that you 3 have to use:

$data = array(1,3,4);
$select->where(array('status_id' => $data));

Result:

WHERE `status_id` IN ('1', '3', '4')

I couldn't find this 2 documented anywhere! ZF documentation is 1 generally sub-optimal.

Score: 8

apparently it is super simple... stupid 1 me:

$select->where('status_id IN(1,3,4)');

:(

Score: 2

We can use Zend\Db\Sql\Predicate\In with Zend\Db\Sql\Where to make a where in query inside 1 a model.

$this->status_ids = array(1,3,4);

// select attributes from db by where in 
$result = $this->select(function (Select $select) {
   $predicate = new In();
   $select->where(
      $predicate->setValueSet($this->status_ids)
                ->setIdentifier('status_id')
      );
})->toArray();
Score: 2
This solution works well with zf2     
 $ids = array('1', '2', '3', '4', '5', '6', '7', '8');
 $select->where(array("app_post_id"=> $ids));

or

 $ids = array('1', '2', '3', '4', '5', '6', '7', '8');
    $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from('app_post_comments');
        $select->where(array("app_post_id"=> $ids));

//        echo $select->getSqlString($this->adapter->getPlatform());
//        exit;
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = new ResultSet();
        $resultSet->initialize($result);
        $resultSet->buffer()->toArray();
        echo '<pre>';
        print_r($resultSet);
        exit;
        return $resultSet;

0

Score: 1
$completionNo = implode(",",$data);

$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$select = $db->select()->from(array("p"=>PREFIX . "property_master"),array('id','completion_no','total_carpet_area'))->where("p.completion_no IN (?)", $completionNo);

0

More Related questions