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?
Oh, it has been solved with this:
$query = $this->getDoctrine()->getEntityManager()->createQueryBuilder()
->select('category, product')
->from('ShopBundle:Categories', 'category')
->join('category.product', 'product');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.