Symfony Doctrine get Many to Many Object (Shop & Category)



我有点丢失。

我想在所有商店和商店的所有商店和商店的所有类别中获取清单。例如 Shopname:休闲,旅行,航班

我认为我在推理方面犯了错误。遵循我的代码:

这是ShopRepository:

public function getCategoryAndShops() {
    return $this 
    -> createQueryBuilder('s') 
    -> select('s, c') 
    -> leftJoin('s.categoryShop', 'c') 
    -> getQuery() -> execute();
}

这是ShopController:

  $em = $this->getDoctrine()->getManager();
    //$entities = $em->getRepository('DbeDonaciBundle:Shop')->findAll();
    $entities = $em->getRepository('DbeDonaciBundle:Shop')->getCategoryAndShops();
    return $this->render('DbeDonaciBundle:Shop:index.html.twig', array(
        'entities' => $entities,
    ));

错误在哪里,索引应该如何?

预先感谢!

由于您在实体中有关系,因此可以选择这样的事情:

$entities = $em->getRepository('DbeDonaciBundle:Shop')->findAll();

和模板中:

{% for entity in entities %}
      {{ entity.name }}:
      <ul>
          {% for cat in entity.categoryShop %}
          <li>{{ cat.name }}</li>
          {% endfor %}
     </ul>
{% endfor %}

最新更新