我想在Zend_Db中生成这个复杂的WHERE子句:
SELECT *
FROM 'products'
WHERE
status = 'active'
AND
(
attribute = 'one'
OR
attribute = 'two'
OR
[...]
)
;
Run Code Online (Sandbox Code Playgroud)
我试过这个:
$select->from('product');
$select->where('status = ?', $status);
$select->where('attribute = ?', $a1);
$select->orWhere('attribute = ?', $a2);
Run Code Online (Sandbox Code Playgroud)
那产生了:
SELECT `product`.*
FROM `product`
WHERE
(status = 'active')
AND
(attribute = 'one')
OR
(attribute = 'two')
;
Run Code Online (Sandbox Code Playgroud)
我确实找到了一种方法来完成这项工作,但我觉得通过先使用PHP组合"OR"子句然后使用Zend_Db where()子句将它们组合起来,这有点"作弊".PHP代码:
$WHERE = array();
foreach($attributes as $a):
#WHERE[] = "attribute = '" . $a . "'";
endforeach;
$WHERE = implode(' OR ', $WHERE);
$select->from('product');
$select->where('status …Run Code Online (Sandbox Code Playgroud)