我想通过给定类别提取产品列表,我不知道该怎么做!我发现findAll会返回数据库中的所有产品,我应该使用dql吗?
像:
我的控制器
public function ProductByCategory(Request $request, Category $cat)
{
$product =new Product() ;
$form = $this->createForm('AppBundle\Form\ProductType',$product);
$query = $em->createQueryBuilder('p')
->where('p->getCategory()->getId() = :categoryid') /*I mean where id
product =id category */
->setParameter(' categoryid ',$cat->getId())
->getQuery();
$products = $query->getResult();
return $this->render('AppBundle:Category:ListProductByCategory.html.twig', array('products' =>$products));
}
我的树枝看起来像是:
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Product name</th>
<th>Product price</th>
<th>quantity</th>
<th>color</th>
</tr>
</thead>
<tbody>
{% for p in products %}
<tr>
<td>{{p.name }}<td>
<td>{{ p.price }}</td>
<td>{{ p. quantity }}</td>
<td>{{ p. color }}</td>
<td>
<a href="{{ path('us_product_update', {'id': p.id}) }}" name="update">Update</a>
<a href="{{ path('us_ product _delete', {'id': p.id}) }}" name="delete">delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
UPADTE
我正在使用正确的代码更新我的帖子,我希望这将有助于sombody
$products= $this->getDoctrine()->getManager()->getRepository('AppBundle:Product')->findByCategory($cat->getId());
答案 0 :(得分:0)
尝试此操作以根据类别ID
获取您的产品$products = $this->manager->getRepository('AppBundle:Product')
->findOneByCategory($cat->getId());