I have a list of elements to display on a page with a search form above it to filter these elements, as in any regular backend. The problem is that I don’t know how to add search criteria to an existing query with joins ... Here is what I have:
I use the specific method in the repository associated with the entity to add joins to the request (to avoid many requests). The controller looks like this:
class ModelController extends Controller
{
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$query = $em->getRepository('AcmeDemoBundle:Item')->getList();
}
}
The method getListin the repository is as follows:
use Doctrine\ORM\EntityRepository;
class ItemRepository extends EntityRepository
{
public function getList()
{
$queryBuilder = $this
->createQueryBuilder('i')
->innerJoin('i.brand', 'b');
return $queryBuilder->getQuery();
}
}
I created a form object ItemSearchTypewith several fields to search for elements.
How can I easily add search criteria from the data presented in the search form to display the filtered items?
This is what is in my controller regarding the search form:
class ModelController extends Controller
{
public function indexAction(Request $request)
{
if ($request->getMethod() === 'POST') {
$searchForm->bindRequest($request);
if ($searchForm->isValid()) {
$searchCriteria = $searchForm->getData();
}
}
}
!