douyi1855 2016-01-26 05:22
浏览 20
已采纳

学说多对多 - 产品和类别

How can I build the search query to search products using category ? Following is my category and product.

This is my product entity

class Product {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    protected $name;


    /**
     * @var \Doctrine\Common\Collections\Collection|UserGroup[]
     *
     * @ORM\ManyToMany(targetEntity="Catalog\Model\Entity\Category", inversedBy="product")
     * @ORM\JoinTable(
     *  name="product_category",
     *  joinColumns={
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     *  }
     * )
     */
    protected $categories;

}

and This is category entity..

class Category {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;
    /**
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    protected $title;

    /**
     * @ORM\ManyToOne(targetEntity="Catalog\Model\Entity\Product", inversedBy="categories")
     */
    /**
     * @var \Doctrine\Common\Collections\Collection|Product[]
     *
     * @ORM\ManyToMany(targetEntity="Catalog\Model\Entity\Product", mappedBy="categories")
     */
    protected $products;
} 

and I am trying to build search query. How I should modify the following query to search using category ?

    $productName = $searchParams['productName'];

    $arrWhere = array();
    $productSql = "SELECT p FROM \Catalog\Model\Entity\Product p";

    if ($productName) {
        array_push($arrWhere, "p.name LIKE '" . '%'.$productName.'%' . "'");
    }


    if (count($arrWhere) > 0) {
        $productSql.= " WHERE " . implode(' AND ',$arrWhere);
    }

    $productSql.= $order_query_str;

    $query = $em->createQuery($productSql);
  • 写回答

1条回答 默认 最新

  • drnysdnnb2909701 2016-01-26 05:43
    关注

    I'd suggest using Doctrine's Query builder.

    $qb = $em->createQueryBuilder();
    $qb->select(‘p’)
        -> from('\Catalog\Model\Entity\Product')
        ->leftJoin('p.categories', 'c');
    
    
    if (isset($searchParams['productName'])) {
        $qb->andWhere('p.name LIKE :prodName')
            ->setParameter('prodName', '%' . $searchParams['productName'] . '%');
    }
    if (isset($searchParams['categoryTitle'])) {
        $qb->andWhere('c.title LIKE :catTitle')
            ->setParameter('catTitle', '%' . $searchParams['categoryTitle'] . '%');
    }
    
    $query = $qb->getQuery();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效