douji0588 2015-03-12 10:44
浏览 17
已采纳

如何从条件中获得另一个实体的结果?

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?

  • 写回答

1条回答 默认 最新

  • dongzhe3171 2015-03-12 11:09
    关注

    Oh, it has been solved with this:

    $query = $this->getDoctrine()->getEntityManager()->createQueryBuilder()
                ->select('category, product')
                ->from('ShopBundle:Categories', 'category')
                ->join('category.product', 'product');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题