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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度