首页 > 解决方案 > 如何在关联ID不相等的Doctrine中查询ManyToMany关系

问题描述

我确实看到了这个问题,并且认为这是我需要的。它与我的要求非常相似。然而,改变一件小事似乎打破了整个逻辑。

所以这是我的架构:

/** @Entity **/
class Product
{    
    /**
     * @ManyToMany(targetEntity="Category", inversedBy="products")
     **/
    private $categories;

    public function __construct() {
        $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

/** @Entity **/
class Category
{
    /**
     * @ManyToMany(targetEntity="Product", mappedBy="categories")
     **/
    private $products;

    public function __construct() {
        $this->products = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

我需要查询所有不属于特定类别的产品。我的尝试是:

// ProductRepository

public function getProducts() {
        return $this->createQueryBuilder('p')
            ->innerJoin('p.categories', 'c')
            ->where('c.id != :id')
            ->setParameter('id', 1)
            ->getQuery()
            ->getResult();
    }

我没有在此处发布我的数据,因为此查询的结果是所有产品。我有大约 5 000 种产品,并且很确定并非所有产品都属于我的类别。我尝试了不同的类别。它显示所有产品不取决于我的查询。

我也在这里留下 SQL 查询:

SELECT p0_.* FROM products p0_ 
INNER JOIN product_categories p2_ ON p0_.id = p2_.product_id
INNER JOIN categories c1_ ON c1_.id = p2_.category_id 
WHERE c1_.id != 1

另一个问题是它给了我产品重复。我可以使用group by p0_.id,但我不知道它是否有效。

我阅读了关于多对多关系以及如何查询它们的谷歌信息,但我发现所有关于平等查询的信息并且我没有平等。

标签: phpdoctrine-ormdoctrine

解决方案


要排除属于第 1 类的所有产品,您可以在学说中使用 not 表达为

$categoryProducts = $this->createQueryBuilder('p1')
                         ->select('p1.id')
                         ->innerJoin('p1.categories', 'c')
                         ->where('c.id = :id')
                         ->setParameter('id', 1)
                         ->getDQL();
$qb =  $this->createQueryBuilder('p');
$products = $qb->where(
                $qb->expr()->not(
                    $qb->expr()->in(
                        'p.id',
                        $categoryProducts
                    )
                )
            )
            ->getQuery()
            ->getResult();

另一种方法但不确定,我想这应该通过使用左连接查询产生相同的结果

$this->createQueryBuilder('p')
    ->leftJoin('p.categories', 'c', 'WITH', 'c.id = :id')
    ->where('c.id IS NULL' )
    ->setParameters('id',1)
    ->getQuery()
    ->getResult();


推荐阅读