2017-08-03 136 views
0

我正在構建一個Web應用程序,用戶發表帖子。該帖子包括類別和位置。我可以單獨查看類別和位置的帖子。過濾項目列表symfony

我想要做的是過濾視圖,只列出一個位置的類別列表。例如當我點擊鞋類我想過濾只顯示美國位置離開其他地點。由於

控制器:

public function catergoryitemsAction($name) 
    { 
    $em = $this->getDoctrine()->getManager(); 

    $category = $em->getRepository('KaftanAvenueBundle:Category')->find($name); 

    return $this->render('@KaftanAvenue/Page/categoryitems.html.twig', 
     ['category' => $category]); 

} 


public function locationitemsAction($name) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $location = $em->getRepository('KaftanAvenueBundle:State')->find($name); 

    return $this->render('@KaftanAvenue/Page/locationitems.html.twig', 
     ['location' => $location]); 

} 

路由:

categoryitems: 
    path: /category/{name} 
    defaults: {_controller: KaftanAvenueBundle:Page:catergoryitems} 


locationitems: 
    path: /location/{name} 
    defaults: {_controller: KaftanAvenueBundle:Page:locationitems} 

樹枝:

{% extends "::base.html.twig" %} 

{% block body %} 
    <div class="container"> 
     {{ category.name }} 
    <div class="row"> 
     {% for entry in category.ads %} 
      <div class="media"> 
       <div class="media-left"> 
        <img src="{{ asset('images/'~ entry.imageName) }}" class="media-object" style="width:100px"> 
       </div> 
       <div class="media-body"> 
        <h4 class="media-heading" ><a style="color: #550000" href="{{ path('view',{id : entry.id}) }}">{{ entry.title }}</a></h4> 
        <p>Price: {{ entry.price }} 
         <br> 
         Created: {{ entry.timestamp | date('d/m/y') }} 
         <br> 
        </p> 
       </div> 
      </div> 
      <hr> 
     {% endfor %} 
    </div> 
</div> 

{% endblock %} 

index.html.twig

{% extends "::base.html.twig" %} 

{% block body %} 

<header class="page-heading"> 

    <div class="container"> 


    </div> 
</header> 


<div class="container marketing"> 

    <div class="row"> 
     <div class="col-lg-2"> 
      <a href="{{ path('categoryitems',{ 'name' : category.id}) }} "> <img class="img-circle" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Generic placeholder image" width="140" height="140"> 
      <h4>Cap</h4></a> 

     </div> 
     <!-- /.col-lg-4 --> 

    </div> 

    </div> 

{%端塊%}

回答

0

答案就在這裏:

$category = $em->getRepository('KaftanAvenueBundle:Category')->find($name); 

爲什麼你會在這樣一個良好的道路停車讓類別,而不是與該類別視圖帖子?

我想通過類似更換:

$category = $em->getRepository('KaftanAvenueBundle:Category')->findOneByName($name); 

$posts = $em->getRepository('KaftanAvenueBundle:Post')->findByCategory($category); 

return $this->render('@KaftanAvenue/Page/categoryitems.html.twig', 
     ['posts' => $posts]); 

請注意,我也不知道你的等級也不郵政的實體,所以它可能是一個有點不同,但這個想法是在這裏。