原则 2 查询产品是否等于多个类别

发布于 2024-12-18 09:06:01 字数 635 浏览 6 评论 0原文

$categories= array(3,20,24);    

$qb = $this->em->createQueryBuilder();
        $qb->select('p')
                ->from('\Entities\Productss', 'p')
                ->leftJoin('p.category', 'c')
                ->andWhere('p.id =?1')
                ->andWhere('p.id =?2')
                 ->andWhere('p.id =?2')
             ->setParameter(1, $categories[0])             
->setParameter(2, $categories[1])
->setParameter(3, $categories[2])

                ->getQuery();

这不允许多个位置...

$categories 是一个数组,其中包含必须匹配的类别才能选择正确的产品。例如鞋子(3),黑色(20),小号(24)

可以吗?

$categories= array(3,20,24);    

$qb = $this->em->createQueryBuilder();
        $qb->select('p')
                ->from('\Entities\Productss', 'p')
                ->leftJoin('p.category', 'c')
                ->andWhere('p.id =?1')
                ->andWhere('p.id =?2')
                 ->andWhere('p.id =?2')
             ->setParameter(1, $categories[0])             
->setParameter(2, $categories[1])
->setParameter(3, $categories[2])

                ->getQuery();

this doesnt allow multiple wheres...

$categories is an array which consist of categories it must match in order to select correct products. such as shoes(3), black(20), small(24)

possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬心 2024-12-25 09:06:01

在 Doctrine 的文档中,我发现了这一点:

// Example - $qb->expr()->in('u.id', array(1, 2, 3))
// Make sure that you do NOT use something similar to $qb->expr()->in('value', array('stringvalue')) as this will cause Doctrine to throw an Exception.
// Instead, use $qb->expr()->in('value', array('?1')) and bind your parameter to ?1 (see section above)
public function in($x, $y); // Returns Expr\Func instance

// Example - $qb->expr()->notIn('u.id', '2')
public function notIn($x, $y); // Returns Expr\Func instance

It should be possible to put a subquery in this function。我自己从未使用过,但请尝试一下。

编辑

我知道这是一个多对多的关系。在这种情况下,您应该使用 MEMBER OF 选项。

所以就像:

$qb->...
   ->andWhere("p.category MEMBER OF ?1")
   ->andWhere("p.category MEMBER OF ?2")
   ->...

In the documentation of Doctrine I found this:

// Example - $qb->expr()->in('u.id', array(1, 2, 3))
// Make sure that you do NOT use something similar to $qb->expr()->in('value', array('stringvalue')) as this will cause Doctrine to throw an Exception.
// Instead, use $qb->expr()->in('value', array('?1')) and bind your parameter to ?1 (see section above)
public function in($x, $y); // Returns Expr\Func instance

// Example - $qb->expr()->notIn('u.id', '2')
public function notIn($x, $y); // Returns Expr\Func instance

It should be possible to put a subquery in this function. I never used it myself, but give it a try.

EDIT

I understand it is a many-to-many relation. In this case you should use the MEMBER OF option.

So like:

$qb->...
   ->andWhere("p.category MEMBER OF ?1")
   ->andWhere("p.category MEMBER OF ?2")
   ->...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文