带有苗条 pdo 的 When 条件中的括号
Parenthesis in When conditions with slim pdo
使用 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
(这些是问题的随机数据)
我在那些 类 中添加了这些方法。
显然,这对您来说扩展 SLIM PDO 本身会更好,而不是添加一些代码。
vendor\slim\pdo\src\PDO\Clause\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;
}
vendor\slim\pdo\src\PDO\Statement\StatementContainer.php
public function openParenthesis() {
$this->whereClause->openParenthesis();
}
public function closeParenthesis() {
$this->whereClause->closeParenthesis();
}
使用 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
(这些是问题的随机数据)
我在那些 类 中添加了这些方法。 显然,这对您来说扩展 SLIM PDO 本身会更好,而不是添加一些代码。
vendor\slim\pdo\src\PDO\Clause\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;
}
vendor\slim\pdo\src\PDO\Statement\StatementContainer.php
public function openParenthesis() {
$this->whereClause->openParenthesis();
}
public function closeParenthesis() {
$this->whereClause->closeParenthesis();
}