2012-08-11 52 views
0

我有實體1:Upload,它有一個oneToMany關係comments的Symfony2/Doctrine2:另一個實體(多對一)的排序依據計數

我有上傳自定義程序存儲庫,我想找到最評論上傳。

所以當我做

/** 
* @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 

似乎並不想接受的意見數量排序,我怎麼能解決這個問題或會產生什麼爲查詢生成器重新編寫適當的代碼?

回答

0

你必須通過「U」作爲參數去你createQueryBuilder()函數,並宣佈你的getWeekTopUploads()函數在您的UploadRepository:

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

return $qb; 
+0

我沒有得到你的代碼示例如何解決我的問題.. ..你的意思是在你的變量$ from添加註釋? – Confidence 2012-08-11 11:24:34

+0

我編輯了我的答案。首先,確保將你的函數放入你的UploadRepository中,在那裏你存儲所有與上傳相關的自定義查詢。然後嘗試我的代碼,我可能犯了錯別字,但精神在這裏。 – guillaumepotier 2012-08-11 11:40:12

相關問題