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')
I would like to share my concat code:
// 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 .'%');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With