2014-02-10 12 views
0

我的教條模型存在問題。當我打電話給他我有一個錯誤,我不知道爲什麼......原則模型中的錯誤

我的實體:

namespace Dimi\YvmBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\ORM\EntityRepository; 

/** 
* Download 
* 
* @ORM\Table("t_download") 
* @ORM\Entity(repositoryClass="Dimi\YvmBundle\Entity\DownloadRepository") 
*/ 
class Download extends EntityRepository 
{ 


    public function getLastDownload() 
    { 

     $em = $this->getEntityManager(); 
     //$em = $this->getDoctrine()->getManager(); 
     $query = $em->createQueryBuilder(); 

     $query->select('d') 
      ->from('DimiYvmBundle:Download', 'd') 
      ->orderBy('d.id', 'DESC') 
      ->groupBy('d.ytId'); 


     $query->setMaxResults(48); 
     return $query->getQuery()->getResult(); 

    } 

} 

TopController.php:

public function getLastDownload() 
    { 

     $query = $this->createQueryBuilder('q'); 

     $query->select('d') 
      ->from('DimiYvmBundle:Download', 'd') 
      ->orderBy('d.id', 'DESC') 
      ->groupBy('d.ytId'); 


     $query->setMaxResults(48); 
     return $query->getQuery()->getResult(); 

    } 

錯誤:

ContextErrorException: Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::__construct(), called in /var/www/site/main.site/Symfony2/src/Dimi/YvmBundle/Controller/TopController.php on line 28 and defined in /var/www/site/main.site/Symfony2/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php line 67 

你知道我該如何解決這個問題嗎?

謝謝大家的幫助。 最好的問候,

編輯:

,如果你想創建你必須將它們寫入到myentitiRepository.php,不能直接在您的myentity.php自定義查詢我已經解決了我的問題,有學說。

+0

請在28行左右顯示您的'TopController.php'。 – NHG

+0

所以,可能問題是'$ em = $ this-> getEntityManager();'。嘗試刪除該行並將'$ query = $ em-> createQueryBuilder();'更改爲'$ query = $ this-> createQueryBuilder('q');'。讓我知道它是否在更改後工作:) – NHG

+0

哦,我點了點頭,忘記了createQueryBuilder()方法的別名參數。所以請添加像'createQueryBuilder('q')''這樣的別名。 – NHG

回答

4

我假定這是原則2,你的發佈版的實體代碼是準確的?

你有:類下載擴展EntityRepository這是錯誤的。

實體不擴展存儲庫。兩個完全不同的對象。

你應該有:

/ ** 
    * Download Entity 
    * 
    * @ORM\Table("t_download") 
    * @ORM\Entity(repositoryClass="Dimi\YvmBundle\Entity\DownloadRepository") 
*/ 
class Download 
{ 
    /* @Id */ 
    protected $id; 

    /* Other property mappings */ 

class DownLoadRepository extends EntityRepository 
{ 
    // Your custom queries 

是的,您的查詢的建築規範需要一些工作。但是先把你的實體和倉庫放在不同的類中。

+0

感謝您的幫助!這很好! – Dimitri

1

因此,首先您必須爲createQueryBuilder()方法(如-createQueryBuilder('q');)定義查詢別名。接下來,您應該使用較短的表示法:$this->createQueryBuilder();而不是$query = $this->getEntityManager()->createQueryBuilder();

比較:getEntityManager()createQueryBuilder($alias)

此外,你應該通過$this->getDoctrine()->getRepository('YourBundleBundle:Download')讓你Download庫在控制器中。當你打電話給new Download()擴展Doctrine\ORM\EntityRepository的構造方法也被調用,它會導致錯誤。

As @Cerad寫道 - EntityRepository是分開的類。在您的控制器後,得到了正確的repository類可以調用每個方法簡單:

$repository = $this->getDoctrine()->getRepository('YourBundleBundle:Download'); 
$result = $repository->myCustomMethod(); 
+0

我已經編輯了我的實體方法,但我仍然有同樣的錯誤... – Dimitri

+0

@Dimi我更新了我的答案 - 你以錯誤的方式得到了你的存儲庫。 – NHG

+0

我修改了我的控制器,但是你知道如何從我的控制器調用我的實體方法嗎? '$ lasdl = $ this-> getDoctrine() - > getRepository('DimiYvmBundle:Download') - > getLastDownload();'? – Dimitri