Doctrine QueryBuilder和concat问题 [英] Doctrine QueryBuilder and concat issues
问题描述
我有以下代码,它依赖于Doctrine的QueryBuilder API来生成DQL statetements。
class PlayerRepository extends EntityRepository
{
public function findByPartialNameMatch($ trainer,$ fullName)
{
$ qb = $ this-> createQueryBuilder('tp');
$ qb-> innerJoin('tp.player','p')
- >其中($ qb-> expr() - > andX(
$ qb-> expr() - > orX(
$ qb-> expr() - > like(
$ qb-> expr() - > concat .firstName',$ qb-> expr() - > concat('','p.lastName')),
$ qb-> expr() - > literal($ fullName。'% ')
),
$ qb-> expr() - > like(
$ qb-> expr() - > concat('p.lastName',$ qb - > expr() - > concat('','p.firstName')),
$ qb-> expr() - > literal($ fullName。'%')
)
),
$ qb-> expr() - > eq('tp.trainer','?1')
)
)
- > groupBy('p.id')
- > orderBy('p.lastName','ASC')
- > orderBy('p.firstName','ASC')
- > setParameter(1,$ trainer);
return $ qb-> getQuery() - > getResult();
}
}
当我运行它时,Symfony2会抛出以下错误消息:
[语法错误]行0,col 123:错误:预期的StateFieldPathExpression |字符串| InputParameter | FunctionReturningStrings |聚合表达式,得到',$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $>>>>>>>>>>>>>>>>>>>>>>>>>>>>
at QueryException :: syntaxError('line 0,col 123:Error:Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression,got',' ')
在D:\Work\vendor\doctrine\lib\Doctrine\ORM\Query\Parser.php第396行 - +
在Parser - > syntaxError ('StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression')
在D:\Work\vendor\doctrine\lib\Doctrine\ORM\Query\Parser.php在第2391行 - +
在Parser - > StringPrimary()
在D:\Work\vendor\doctrine\lib\Doctrine\ORM\Query\AST\Functions\ConcatFunction .php在第60行 - +
在ConcatFunction - > parse(object(Parser))
在D:\Work\vendor\\ doctrine\lib\Doctrine\ORM\Query\Parser.php行2852 -
从上面可以看出,这个问题在某种程度上与concat helper函数有关,并且该函数期望枚举输入,但是以某种方式(?)收到一个逗号(,)。
上面的代码有什么问题?
感谢您的帮助!
解决方案问题在于这个部分:
$ qb-> expr() - > ; concat('','p.lastName')
你不能只是放置空格,因为doctrine期望一些识别者在这里。尝试这样做:
$ qb-> expr() - > concat($ qb-> expr() > literal(''),'p.lastName')
I have the following code, which relies on Doctrine's QueryBuilder API to generate DQL statetements.
class PlayerRepository extends EntityRepository
{
public function findByPartialNameMatch($trainer, $fullName)
{
$qb = $this->createQueryBuilder('tp');
$qb->innerJoin('tp.player', 'p')
->where($qb->expr()->andX(
$qb->expr()->orX(
$qb->expr()->like(
$qb->expr()->concat('p.firstName', $qb->expr()->concat(' ', 'p.lastName')),
$qb->expr()->literal($fullName.'%')
),
$qb->expr()->like(
$qb->expr()->concat('p.lastName', $qb->expr()->concat(' ', 'p.firstName')),
$qb->expr()->literal($fullName.'%')
)
),
$qb->expr()->eq('tp.trainer', '?1')
)
)
->groupBy('p.id')
->orderBy('p.lastName', 'ASC')
->orderBy('p.firstName', 'ASC')
->setParameter(1, $trainer);
return $qb->getQuery()->getResult();
}
}
When I run it, Symfony2 throws the following error message:
[Syntax Error] line 0, col 123: Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression, got ','
A look at the stack trace, reveals the following:
at QueryException ::syntaxError ('line 0, col 123: Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression, got ','')
in D:\Work\vendor\doctrine\lib\Doctrine\ORM\Query\Parser.php at line 396 -+
at Parser ->syntaxError ('StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression')
in D:\Work\vendor\doctrine\lib\Doctrine\ORM\Query\Parser.php at line 2391 -+
at Parser ->StringPrimary ()
in D:\Work\vendor\doctrine\lib\Doctrine\ORM\Query\AST\Functions\ConcatFunction.php at line 60 -+
at ConcatFunction ->parse (object(Parser))
in D:\Work\vendor\doctrine\lib\Doctrine\ORM\Query\Parser.php at line 2852 -
From the above, I understand that the issue is somehow related to the concat helper function, and that the function expects the enumerated input but somehow(?) received a comma (,).
What is wrong with the code above? Hours of search could not shed a light into the problem.
Thank you for all your help!
解决方案 The problem is about this part:
$qb->expr()->concat(' ', 'p.lastName')
You cannot just put space as doctrine expects some identificator here. Try this instead:
$qb->expr()->concat($qb->expr()->literal(' '), 'p.lastName')
这篇关于Doctrine QueryBuilder和concat问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!