【发布时间】:2010-12-07 23:05:25
【问题描述】:
我想在 Zend_Db 中生成这个复杂的 WHERE 子句:
SELECT *
FROM 'products'
WHERE
status = 'active'
AND
(
attribute = 'one'
OR
attribute = 'two'
OR
[...]
)
;
我试过了:
$select->from('product');
$select->where('status = ?', $status);
$select->where('attribute = ?', $a1);
$select->orWhere('attribute = ?', $a2);
并且产生了:
SELECT `product`.*
FROM `product`
WHERE
(status = 'active')
AND
(attribute = 'one')
OR
(attribute = 'two')
;
我确实想出了一种方法来完成这项工作,但我觉得这是一种“作弊”,首先使用 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 = ?', $status);
$select->where($WHERE);
这产生了我正在寻找的东西。但我很好奇是否有使用 Zend_Db 工具而不是先在 PHP 中组合它来获取复杂的 WHERE 语句(实际上并不太复杂,只是添加一些括号)的“官方”方法。
干杯!
【问题讨论】:
标签: php sql zend-framework zend-db