2012-05-14 108 views
5

我有以下代碼,它依靠Doctrine的QueryBuilder API來生成DQL陳述。Doctrine QueryBuilder和concat問題

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 helper函數有關,而函數expec列舉的輸入,但不知何故(?)收到一個逗號(,)。

上面的代碼有什麼問題?搜尋小時數不能解決問題。

謝謝你的幫助!

回答

24

的問題是關於這一部分:

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

你不能只是把空間學說在這裏希望一些識別器。試試這個:

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

事實上它的工作。 [Doctrine documentation](http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/query-builder.html#the-expr-class),它提供了一個例子這是不準確的。 –

2

我想和大家分享我的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 .'%');