[SOLVED] Syntax error when using QueryBuilder and ->iLike()

Currently I am writing a NextCloud app on my own. Everything’s fine so far, but there is one thing I cannot solve and its driving me nuts.

I have the following code …

$ors = $qb->expr()->orX(
  $qb->expr()->iLike(
    "first_name",
    $qb->createNamedParameter(
      sprintf( "%%%s%%", $searchTerm )
    )
  ),
  $qb->expr()->iLike(
    "last_name",
    $qb->createNamedParameter(
      sprintf( "%%%s%%", $searchTerm )
    )
  ),
  $qb->expr()->iLike(
    "birth_date",
    $qb->createNamedParameter(
      sprintf( "%%%s%%", $searchTerm )
    )
  )
);

$qb
  ->select( "*" )
  ->from( $this->getTableName() )
  ->where( $ors )
  ->setMaxResults( $opts["limit"] )
  ->setFirstResult( $opts["offset"] );

… that generates the following SQL query:

SELECT *
FROM `oc_projects_persons`
WHERE
  (`first_name`  COLLATE utf8mb4_general_ci LIKE :dcValue1) OR
  (`last_name`  COLLATE utf8mb4_general_ci LIKE :dcValue2) OR
  (`birth_date`  COLLATE utf8mb4_general_ci LIKE :dcValue3)

But I get a Doctrine\DBAL\Exception\SyntaxErrorException starting at :dcValue1. Any ideas how to solve this? Do I use the QueryBuilder wrong?

Okay, so I found the solution by myself. The problem was, that in the actual code I used two different instances of QueryBuilder.

1 Like