我有以下疑问:
$queryBuilder = $em->createQueryBuilder()->select('s')
->from("MainBundle:Style", 's')
->select('DISTINCT s')
->leftJoin('s.pictureStyle', 'ps')
->leftJoin('ps.picture', 'p')
->leftJoin('p.category', 'pc');
if ($category instanceof InstagramTopCategory)
{
$queryBuilder->leftJoin('pc.pictureTopCategory', 'category');
}
else if ($category instanceof InstagramFirstLevelCategory)
{
$queryBuilder->leftJoin('pc.pictureFirstLevelCategory', 'category');
}
else if ($category instanceof InstagramSecondLevelCategory)
{
$queryBuilder->leftJoin('pc.pictureSecondLevelCategory', 'category');
}
$query = $queryBuilder->where('category = :category')
->andWhere('p.isLocked = 0')
->andWhere('p.deletedAt IS NULL')
->orderBy('COUNT(p.id)', 'DESC')
->groupBy('ps.pictureStyle')
->setParameter('category', $category)
->setMaxResults(10)
->getQuery();
但是,按顺序排列,它不会给我一个错误:
Error: Expected end of string, got '('
这怎么可能?我在MySQL中直接使用COUNT在orderBy中编写了一个等价的查询,它运行得很好,这是doctrine中的问题吗?
编辑:
100%确定是订单,因为如果我删除行,错误就会消失
最佳答案
按如下方式修改查询
$queryBuilder = $em->createQueryBuilder()
->select('s')
->from("MainBundle:Style", 's')
->select('DISTINCT s')
->addSelect('COUNT(p.id) as HIDDEN c_id')
[...]
->orderBy('c_id', 'DESC')
[...]
关于mysql - ORDER BY Count(p.id)有可能吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27327764/