php - 如何在关联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
,但我不知道它是否有效。
我阅读了关于多对多关系以及如何查询它们的谷歌信息,但我发现所有关于平等查询的信息并且我没有平等。
解决方案
要排除属于第 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();
推荐阅读
- php - 如何在标签表中添加 tr:nth-child?
- php - PHP将UTC中的字符串日期时间转换为GMT+7格式
- javascript - C++ uWebSockets 服务器可以与 JavaScript 客户端通信吗?
- ios - Xcode 10 中的 Cocoa pod 文件“XLPagerTabStrip”出错
- keras - 我可以使用已经通过 model.fit() 运行的模型历史文件获取其他指标值吗?
- django-templates - 如何在 Django 2 中登录后将用户和管理员重定向到不同的模板
- c - __malloc_hook 奇怪的行为
- android - 将存储在 MySql 数据库中的图像名称转换为 Android 中的 Drawable
- react-native - 在 react-native 中在 javascript 和本机代码(iOS - obj c,Android - java)之间传递数据?
- android - 恐慌:缺少“x86”CPU 的模拟器引擎程序。[Ionic Cordova 运行 android]