Extracting bu category product list using doctrine

I want to extract a list of products using a given category , I don't know how! I found findAll which return all products in the database, should I use dql?

somthing like:

my controller

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));

}

      

my branch will look like this:

<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

I am updating my post with the correct code. I hope this helps someone.

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

      

+3


source to share


1 answer


try this to get your products based on category id



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

      

0


source







All Articles