I have two Entity. The first one is the Category and the second is the Product. They are in ManytoOne and OneToMany relation with each other.
$query = $repository->createQueryBuilder('category')->join('category.product', 'p');
if ($request->request->get("beginDate") != "") {
$query->andWhere('p.date >= :beginDate')
->setParameter('beginDate', $request->request->get("beginDate"));
}
if ($request->request->get("endDate") != "") {
$query->andWhere("p.date <= :endDate")
->setParameter('endDate', $request->request->get("endDate"));
}
$categories = $query->getQuery()->getResult();
foreach($categories as $category){
echo "Category: ".$category->getCategoryName()."<br />";
foreach($category->getProduct() as $product){
echo "Product: ".$product->getProductName()."<br />";
}
}
My problem is, when I get the products, then it prints the all products to the category. I think it's normal when you use OneToMany relation. But i would like to get those products from the categories, where the conditions are trues. Maybe i should make another querybuilder? But then i should copy the conditions. Is there other solution?