按关系字段计数排序
Order by count of relational field
我有以下关系:
Member
一对多Product
Product
多对一 Member
这是我有效的查询:
$qb = $this->createQueryBuilder('m');
$qb
->where('m.Authority = :Authority')
->setParameter('Authority', Authority::CREATOR)
->innerJoin('m.Product', 'p')
->select('COUNT(p) AS HIDDEN item_count', 'm')
->groupBy('m')
->orderBy("item_count", "ASC");
然而,当我为产品状态添加额外的参数时
$qb = $this->createQueryBuilder('m');
$qb
->where('m.Authority = :Authority')
->setParameter('Authority', Authority::CREATOR)
->andWhere('p.Status = :Status')
->setParameter('Status', 1);
->innerJoin('m.Product', 'p')
->select('COUNT(p) AS HIDDEN item_count', 'm', 'p')
->groupBy('p')
->orderBy("item_count", "ASC");
订单不再发生。我转储了 item_count
的值,它始终是 1
。有没有人遇到过同样的问题?此处的目标是获取状态为 1 且按产品数量排序的产品的成员列表。
编辑:
如果我将 groupBy('p')
更改为 groupBy('m')
,它只会为每个 Member
加载一个相关的 Product
对于遇到同样问题的任何人,我就是这样做的:
$qb = $this->createQueryBuilder('m');
$qb
->where('m.Authority = :Authority')
->setParameter('Authority', Authority::CREATOR)
->andWhere('p.Status = :Status')
->setParameter('Status', 1);
->select('p, m')
->addSelect('(SELECT COUNT(DISTINCT p1)
FROM Eccube\Entity\Member m1
INNER JOIN Eccube\Entity\Product p1
WHERE m1.Authority = ' . Authority::CREATOR . ' AND p1.Status = 1 AND p1.Creator = m.id
) AS hidden item_count')
->innerJoin('m.Product', 'p')
->groupBy('p')
->orderBy("item_count", "ASC");
有效,但我不明白为什么会这样 p1.Creator = m.id
?如果我使用 m1.id
,它只会总结项目数。
编辑:在 Jakumi 关于加入的评论之后添加了更新的代码。尽管两个代码都有效,但第二个代码更有意义。
$qb = $this->createQueryBuilder('m');
$qb
->where('m.Authority = :Authority')
->setParameter('Authority', Authority::CREATOR)
->andWhere('p.Status = :Status')
->setParameter('Status', 1);
->select('p, m')
->addSelect('(SELECT COUNT(DISTINCT p1)
FROM Eccube\Entity\Member m1
INNER JOIN m1.Product p1
WHERE m1.Authority = ' . Authority::CREATOR . ' AND p1.Status = 1 AND p1.Creator = m
) AS hidden item_count')
->innerJoin('m.Product', 'p')
->groupBy('p')
->orderBy("item_count", "ASC");
我有以下关系:
Member
一对多Product
Product
多对一 Member
这是我有效的查询:
$qb = $this->createQueryBuilder('m');
$qb
->where('m.Authority = :Authority')
->setParameter('Authority', Authority::CREATOR)
->innerJoin('m.Product', 'p')
->select('COUNT(p) AS HIDDEN item_count', 'm')
->groupBy('m')
->orderBy("item_count", "ASC");
然而,当我为产品状态添加额外的参数时
$qb = $this->createQueryBuilder('m');
$qb
->where('m.Authority = :Authority')
->setParameter('Authority', Authority::CREATOR)
->andWhere('p.Status = :Status')
->setParameter('Status', 1);
->innerJoin('m.Product', 'p')
->select('COUNT(p) AS HIDDEN item_count', 'm', 'p')
->groupBy('p')
->orderBy("item_count", "ASC");
订单不再发生。我转储了 item_count
的值,它始终是 1
。有没有人遇到过同样的问题?此处的目标是获取状态为 1 且按产品数量排序的产品的成员列表。
编辑:
如果我将 groupBy('p')
更改为 groupBy('m')
,它只会为每个 Member
Product
对于遇到同样问题的任何人,我就是这样做的:
$qb = $this->createQueryBuilder('m');
$qb
->where('m.Authority = :Authority')
->setParameter('Authority', Authority::CREATOR)
->andWhere('p.Status = :Status')
->setParameter('Status', 1);
->select('p, m')
->addSelect('(SELECT COUNT(DISTINCT p1)
FROM Eccube\Entity\Member m1
INNER JOIN Eccube\Entity\Product p1
WHERE m1.Authority = ' . Authority::CREATOR . ' AND p1.Status = 1 AND p1.Creator = m.id
) AS hidden item_count')
->innerJoin('m.Product', 'p')
->groupBy('p')
->orderBy("item_count", "ASC");
有效,但我不明白为什么会这样 p1.Creator = m.id
?如果我使用 m1.id
,它只会总结项目数。
编辑:在 Jakumi 关于加入的评论之后添加了更新的代码。尽管两个代码都有效,但第二个代码更有意义。
$qb = $this->createQueryBuilder('m');
$qb
->where('m.Authority = :Authority')
->setParameter('Authority', Authority::CREATOR)
->andWhere('p.Status = :Status')
->setParameter('Status', 1);
->select('p, m')
->addSelect('(SELECT COUNT(DISTINCT p1)
FROM Eccube\Entity\Member m1
INNER JOIN m1.Product p1
WHERE m1.Authority = ' . Authority::CREATOR . ' AND p1.Status = 1 AND p1.Creator = m
) AS hidden item_count')
->innerJoin('m.Product', 'p')
->groupBy('p')
->orderBy("item_count", "ASC");