9

我有以下代码,它依赖于 Doctrine 的 QueryBuilder API 来生成 DQL 语句。

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();
}

}

当我运行它时,Symfony2 会抛出以下错误消息:

[Syntax Error] line 0, col 123: Error: Expected StateFieldPathExpression | string |      InputParameter | FunctionsReturningStrings | AggregateExpression, got ',' 

查看堆栈跟踪,显示以下内容:

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  -

从上面,我了解到该问题与 concat 辅助函数有关,并且该函数需要枚举输入,但不知何故(?)收到逗号(,)。

上面的代码有什么问题?数小时的搜索无法找出问题所在。

谢谢你的帮助!

4

2 回答 2

32

问题在于这部分:

$qb->expr()->concat(' ', 'p.lastName')

您不能像学说在这里需​​要一些标识符一样放置空间。试试这个:

$qb->expr()->concat($qb->expr()->literal(' '), 'p.lastName')
于 2012-05-14T19:01:21.940 回答
3

我想分享我的 concat 代码:

// It is easy to use array to project concat result. It will look like 'Diego Maradona Maradona Diego'
$concatFields = array(
    'p.firstName',
    'p.lastName',
    'p.lastName',
    'p.firstName',
);

// Routine code. All fields will be separated by ' '.
foreach ($concatFields as $field) {
    if (!isset($searchIn)) {
        $searchIn = $qb->expr()->concat($qb->expr()->literal(''), $field);
        continue;
    }

    $searchIn = $qb->expr()->concat(
        $searchIn,
        $qb->expr()->concat($qb->expr()->literal(' '), $field)
    );
}

// Just use concat result to search in.
$anyKeyword = 'ego';
$qb->add('where', $qb->expr()->like($searchIn, ':keyword'));
$qb->setParameter('keyword', '%'. $anyKeyword .'%');
于 2014-03-04T09:02:55.073 回答