Symfony2 / Doctrine2:另一个实体的OrderBy计数(ManytoOne)

时间:2012-08-11 10:59:28

标签: symfony doctrine-orm

我有实体1:Upload,它与oneToMany的关系comments

我有一个上传的自定义存储库,我想找到评论最多的上传。

所以当我做的时候

/**
 * @param $sortBy String
 * @param $limit Int
 */
public function getWeekTopUploads($sortBy,$limit){



    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb->add('select', 'u')
        ->add('from', 'TrackerMembersBundle:Upload u')
        // ->where('u.created')
        ->orderBy('u.comments', 'DESC')
        ->setMaxResults( $limit );


    $query = $qb->getQuery();

    $result = $query->getResult();

    return $result;



}

我收到错误:

[Semantical Error] line 0, col 55 near 'comments DES': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.
500 Internal Server Error - QueryException

似乎不想接受按注释数量排序,如何解决这个问题或者为查询构建器重新编写什么?

1 个答案:

答案 0 :(得分:0)

你必须将'u'作为param传递给你的createQueryBuilder()函数,并在你的UploadRepository中声明你的getWeekTopUploads()函数:

$qb = $this->createQueryBuilder('u');
        ->where('u.created' >= new \DateTime("1 week ago"))
        ->orderBy('u.comments', 'DESC')
        ->setMaxResults($limit)
        ->getQuery()->getResult();

return $qb;
相关问题