buildTermsFromSearchString($search); if ([] === $terms) { return []; } $qb = $this->connection->createQueryBuilder(); $qb->from('chill_main_postal_code', 'cmpc') ->join('cmpc', 'view_chill_main_address_reference', 'vcmar', 'vcmar.postcode_id = cmpc.id') ->join('vcmar', 'country', 'country', condition: 'cmpc.country_id = country.id') ->setFirstResult($firstResult) ->setMaxResults($maxResults) ; $qb->select( 'DISTINCT ON (cmpc.code, cmpc.label) cmpc.id AS postcode_id', 'cmpc.code AS code', 'cmpc.label AS label', 'country.id AS country_id', 'country.countrycode AS country_code', 'country.name AS country_name' ); $paramId = 0; foreach ($terms as $term) { $qb->andWhere('vcmar.address like ?'); $qb->setParameter(++$paramId, "%{$term}%"); } $result = $qb->executeQuery(); foreach ($result->iterateAssociative() as $row) { yield [...$row, 'country_name' => json_decode($row['country_name'], true, 512, JSON_THROW_ON_ERROR)]; } } private function buildTermsFromSearchString(string $search): array { return array_filter( array_map( static fn (string $term) => trim($term), explode(' ', $search) ), static fn (string $term) => '' !== $term ); } }