喜 我必须筑巢一些或/和条件 但我需要在我的sql语句中使用括号以正确的顺序执行此操作 但你怎么做这个
应该采用这种形式 (......或......)和......
日Thnx
答案 0 :(得分:4)
根据此博客文章“Solving the Doctrine Parenthesis Problem”,您需要执行$query->where("(ConditionA OR ConditionB) AND ConditionC");
这可能看起来像:
Doctrine_Query::create()
->from(...)
->where('A = ? OR B = ?', array(valA, valB))
->andWhere('C = ?', valC);
然而,通过扩展whereParenWrap()
,海报确实提供了更通用的解决方案Doctrine_Query
:
DQ::create()
->from(...)
->where('A = ?', valA)
->orWhere('B = ?', valB)
->whereParenWrap()
->andWhere('C = ?', valC);
答案 1 :(得分:0)
Addon的答案:Brackets in Doctrine query
/*
* This is a simple short-hand wrapper for Doctrine_Query. It provides
* a shorter class name and a few additional functions.
*/
class DQ extends Doctrine_Query
{
/**
* Returns a DQ object to get started
*
* @return DQ
*/
public static function create($conn = null, $class = null) {
return new DQ($conn, $class);
}
/**
* This function will wrap the current dql where statement
* in parenthesis. This allows more complex dql statements
* It can be called multiple times during the creation of the dql
* where clause.
*
* @return $this
*/
public function whereParenWrap() {
$where = $this->_dqlParts['where'];
if (count($where) > 0) {
array_unshift($where, '(');
array_push($where, ')');
$this->_dqlParts['where'] = $where;
}
return $this;
}
}
?>
略有修改。 取自:https://gist.github.com/888386/634c51993aaf16565690be10da7b8f13a227020a
答案 2 :(得分:0)
现在您可以使用Expr :: orX()
/** To combine all the OR conditions in one parenthesis, we will collect the conditions in one array */
$orX = [];
foreach ($dto->userNameSearchPhrases as $key => $userNameSearchPhrase) {
$orX[] = $qb->expr()->like('us.username', ':userNameSearchPhrase' . $key);
$qb->setParameter('userNameSearchPhrase' . $key, $userNameSearchPhrase);
}
/** To pass parameters to the Expr::orX() method from an array, use ReflectionMethod */
$reflectionMethod = new \ReflectionMethod(\Doctrine\ORM\Query\Expr::class, 'orX');
$orXObject = $reflectionMethod->invokeArgs($qb->expr(), $orX);
$qb->andWhere($orXObject);