使用学说按类别提取产品列表

Extracting a list of products bu category using doctrine

我想按给定类别提取产品列表,我不知道该怎么做!我找到了 findAll return 我应该使用数据库中的所有产品吗?

类似的东西:

我的控制器

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>

更新

我正在用正确的代码更新我的 post 我希望这会对某人有所帮助

    $products= $this->getDoctrine()->getManager()->getRepository('AppBundle:Product')->findByCategory($cat->getId());

试试这个,根据类别 ID 获取您的产品

$products = $this->manager->getRepository('AppBundle:Product')
            ->findOneByCategory($cat->getId());