Reputation: 33
I have the following Zend SQL query:
$select = $this->select()
->where($this->_quote('VerDate <= ?', $todate))
->where($this->_quote('VerFieldId = ?', $fieldid));
if (count($records) > 0) {
$this->select()->where($this->_quote('VerRecordId IN (?)', $records));
}
$this->select()->group('VerRecordId');
I only want to add the where condition on VerRecordId if an array of $records has been passed to the function.
When I run the query at the moment it stops after the where clause on VerFieldId.
Can anyone help with what I need to do to amend the query?
UPDATE:
I have solved the issue using the following code. However, if anyone knows if this can be structured better, please let me know.
$where = array();
$where['VerDate <= ?'] = $todate;
$where['VerFieldId = ?'] = $fieldid;
if (count($records) > 0) {
$where['VerRecordId IN (?)'] = $records;
}
$select = $this->select();
$this->_where($select,$where);
$select->group('VerRecordId');
This allowed me to conditionally add in the VerRecordId condition.
Upvotes: 2
Views: 1456
Reputation: 461
ZF2
$sql = new Sql($this->adapter);
$select = new Select($this->table);
$where = new \Zend\Db\Sql\Where();
$where->equalTo('field1',(string) $value1);
$where->greaterThan('field2','value2') ;
$where->equalTo('field3','value3');
$select->where($where);
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
ZF1
$select = $this->select();
$select->setIntegrityCheck(false) // if you have joins
->from(array('tablename' => 'tablename',))
$select->where('field = ?', $value);
$select->where(new Zend_Db_Expr("field <> 'value'"));
$result = $this->fetchAll($select);
and then you can loop the $result
i hope it helps you
http://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html
Upvotes: 1