I have the following select:
$select = self::getConnection()->select()
->setSqlCalcFoundRows(true)
->from(array('person' => parent::$_prefix . self::$_tableName), $rows)
->where($wherePerson['statement'], $wherePerson['value'])
->where($whereOwner['statement'], $whereOwner['value'])
->order($sort .' '. $sortDir)
->limit($limit, $offset);
Now i want to add an additional AND
. But only if the field has a value (completed by the user). So when the owner field is empty, may not be the result of my query (see the AND):
SELECT SQL_CALC_FOUND_ROWS xxx
FROM xxxx AS xxxx
WHERE (person_id = '305000270002')
AND (owner = '')
ORDER BY xxxx ASC
LIMIT 25
I don't need the AND if it is empty, otherwise my result is wrong.
Try the following:
$select = self::getConnection()->select()
->setSqlCalcFoundRows(true)
->from(array('person' => parent::$_prefix . self::$_tableName), $rows)
->where($wherePerson['statement'], $wherePerson['value'])
->where($whereOwner['statement'], $whereOwner['value'])
->order($sort .' '. $sortDir)
->limit($limit, $offset);
if (false === empty($theFieldToCheck)) {
// add additional WHERE part if
// a field IS NOT empty
$select->where($additionalWherePart);
}
That creates that base statement and can be extended later by where() method on specific conditions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.