Answer the question
In order to leave comments, you need to log in
How to reduce the number of queries when selecting products on a page in a One to One relationship?
There are two entities, Catalog and CatalogPrice, a one-to-one relationship. I make a selection with pagination like this:
/**
* @Route("/{id}/{page}", name="catalog_by_category", requirements={"id"="\d+"}, defaults={"page"=1})
*/
public function getByCat(EntityManagerInterface $em, $id, $page, Request $request)
{
$query = $em->getRepository(Catalog::class)->findByCategory($id);
$currentCategory = $em->getRepository(Categories::class)->find($id);
$limit = 50;
$paginator = new Paginator($query, $fetchJoinCollection = false);
$pageCount = ceil(count($paginator) / $limit);
$pages = range(1, $pageCount);
$catalog = $query
->setFirstResult($limit * ($page-1))
->setMaxResults($limit)
->getQuery()->getResult();
dump($catalog);
return $this->render('catalog/index.html.twig', [
'catalog' => $catalog,
'pages' => $pages,
'category' => $currentCategory,
'categoryId' => $id,
]);
}
public function findByCategory($id)
{
$query = $this->createQueryBuilder('product')
// ->join('product.catalogPrice', 'catalogPrice')
->where('product.category = :id')
->setParameter('id', $id);
return $query;
}
$catalog = $em->getRepository(Catalog::class)->findBy(['category' => $id], [], 50);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question