使用PHP Slim PDO如何表达:
(条件1或条件2)AND(条件3或条件4)
那就是那样的(但显然这段代码不起作用):
$pdo->select()->from('items')
->whereOpenParenthesis()
->where('color', 'red','OR')
->where('size', 'xl')
->whereCloseParenthesis()
->whereOpenParenthesis()
->where('stock', '10','OR')
->where('price', '99')
->whereCloseParenthesis();
没有原始SQL
(这些是关于问题解释的随机数据)
答案 0 :(得分:0)
我在这些类中添加了这些方法。 显然,扩展SLIM PDO本身会更好,而不是添加一些代码。
供应商\苗条\ PDO \ SRC \ PDO \第\ WhereClause.php
public function openParenthesis(){
$this->container[] = ' ( ';
}
public function closeParenthesis(){
$this->container[] = ' ) ';
}
//Function amended :
/**
* @return string
*/
public function __toString()
{
if (empty($this->container)) {
return '';
}
$args = array();
foreach ($this->container as $where) {
$args[] = $where;
}
$str = implode('', $args);
$str = str_replace('( AND', ' AND ( ', $str);
$str = str_replace('( OR', ' OR ( ', $str);
$str = ' WHERE '.ltrim($str, ' AND');
return $str;
}
供应商\苗条\ PDO \ SRC \ PDO \声明\ StatementContainer.php
public function openParenthesis() {
$this->whereClause->openParenthesis();
}
public function closeParenthesis() {
$this->whereClause->closeParenthesis();
}