I have an object Service
with fields title
, tags
and description
. When searching for a service with QueryBuilder, I can get results sorted by field priority. For example, when I search for a term php
, I want to get services with php
in their header at the top of the list, then services with php
in my tags and services with the search term in their description as the last.
This is part of my Querybuilder:
$qb = $this->createQueryBuilder('service');
$qb->leftJoin('service.tags', 'tag');
$conditions = array($conditions[] = $qb->expr()->eq('service.enabled', true));
$conditions[] = $qb->expr()->eq('service.category', $categoryId);
$term = '%' . $term . '%';
$conditions[] = $qb->expr()->orX(
$qb->expr()->like('service.title', "'$term'"),
$qb->expr()->like('service.description', "'$term'"),
$qb->expr()->like('tag.name', "'$term'")
);
$conditions = call_user_func_array(array($qb->expr(), 'andX'), $conditions);
$qb->where($conditions);
source
share