Posts

Showing posts with the label Optimization

Doctrine Query Builder Optimization

The article describes how to reduce the number of Doctrine generated SQL queries when using Query Builder. The Product entity, which has the required Brand and Category entities and the optional Photo entity: #[ORM\Entity(repositoryClass: ProductRepository::class)] class Product { #[ORM\ManyToOne()] #[ORM\JoinColumn(nullable: false)] private ?Brand $brand = null; #[ORM\ManyToOne(inversedBy: 'products')] #[ORM\JoinColumn(nullable: false)] private ?Category $category = null; #[ORM\OneToOne()] private ?Photo $photo = null; } Product entity repository: class ProductRepository extends ServiceEntityRepository { public function findByCategoryId(int $categoryId): array { return $this->createQueryBuilder('p') ->andWhere('p.category = :categoryId') ->setParameter('categoryId', $categoryId) ->getQuery() ->getResult() ; } } Gettin...