【问题标题】:Extracting a list of products bu category using doctrine使用学说按类别提取产品列表
【发布时间】:2017-11-08 13:35:05
【问题描述】:

我想按给定类别提取产品列表,我不知道该怎么做!我发现 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>

更新

我正在使用正确的代码更新我的帖子,希望对 sombody 有所帮助

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

【问题讨论】:

  • {% for category in p.category %}{{ .category.name }}{% endfor %}

标签: php mysql doctrine symfony-3.2


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2019-12-22
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多