TYPO3 v12 Repository, query with constraints

637 Views Asked by At

Since TYPO3 v12 the use of constraints in the repository query is not working like before.

public function findByDate(
   $dateFrom = null, 
   $dateTill = null, 
   $isAdmin = false, 
   $tags = [], 
   $keyword = ''
): QueryResultInterface
{
   $query = $this->createQuery();
   $query->getQuerySettings()->setIgnoreEnableFields(TRUE);
   $query->getQuerySettings()->setEnableFieldsToBeIgnored(['starttime','endtime']);

   $constraints = [];
   if ($keyword != '') {
      $constraints[] = $query->like('titel', '%'.$keyword.'%' );
   }else{
      if (isset($dateFrom) && isset($dateTill)) {
         $constraints[] = $query->logicalAnd(
            $query->lessThan('startdate', $dateTill->getTimestamp()),
            $query->greaterThan('startdate', $dateFrom->getTimestamp()),
         );
      }
   }

   if (count($tags) > 0) {
      $constraints[] = $query->logicalOr(
         $query->in('tags.uid', $tags)
      );
   }

   if ($isAdmin == false) {
      $constraints[] = $query->equals('share', true);
   }

   $query->matching($query->logicalAnd($constraints));
   return $query->execute();
}

Error

TYPO3\CMS\Extbase\Persistence\Generic\Query::logicalAnd(): Argument #1 must be of type TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface, array given

output of the constraints:

Is there still a way to use constraints?

  • Query with one condition is working
  • LogicalAnd with 2 constraints directly also works
2

There are 2 best solutions below

0
Stefan Bürk On

The signature has changed. Instead of putting the array of constraints to logicalAnd() use the spread operator ....

Replace

$query->matching($query->logicalAnd($constraints));

with

$query->matching($query->logicalAnd(...array_values($constraints)));

EDIT 1

Added array_values() guard to avoid associative key unpacking with newer PHP versions.

1
Carlos Sampaio Peredo On

Found the error - leaving the Brackets the datatype will be Array.

$constraints[] = $query->like('titel', '%'.$keyword.'%' );

instead:

$constraints = $query->like('titel', '%'.$keyword.'%' );

Datatype has to be

\TYPO3\CMS\Extbase\Persistence\Generic\Qom\LogicalAnd