2013-03-02 61 views
7

我是symfony2的新手,我正在嘗試創建定製存儲庫類並且無法做到這一點。Symfony2定製存儲庫類

下面是我在做什麼:

  • 我添加批註的實體類(MobilePhones)

@ORM \實體(repositoryClass = 「Maak \ DefaultBundle \實體\ MobilePhonesRepository」 )

  • 在MobilePhonesRepository我的CR eated我的自定義函數命名findAllMobilePhones()

  • 在控制器我使用調用的函數:

$ EM-> getRepository( 'MaakDefaultBundle:MobilePhones') - > findAllMobilePhones();

但我得到Undefined method findAllMobilePhones(),我已清除緩存並嘗試過但錯誤相同。哪裏不對?

我的倉儲類:

<?php 

namespace Maak\DefaultBundle\Entity; 
use Doctrine\ORM\EntityRepository; 

class MobilePhonesRepository extends EntityRepository 
{ 
    public function findAllMobilePhones() 
    { 
     return $this->getEntityManager()->createQuery('SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC') ->getResult(); 
    } 
} 
+1

是什麼'回聲get_class($ EM-> getRepository( 'MaakDefaultBundle:MobilePhones'))'返回?你還可以提供MobilePhonesRepository的類文件嗎? – Sean 2013-03-02 16:33:39

+0

它返回Doctrine \ ORM \ EntityRepository – 2013-03-02 16:56:34

+0

<?php namespace Maak \ DefaultBundle \ Entity; 使用Doctrine \ ORM \ EntityRepository; 類MobilePhonesRepository擴展EntityRepository { 公共職能findAllMobilePhones(){ 回報 $這個 - > getEntityManager() - >的createQuery( '選擇P檔FROM AcmeStoreBundle:產品p ORDER BY p.name ASC') - >的getResult (); } } – 2013-03-02 16:58:00

回答

2

感謝所有球員爲自己的時間。最後我發現了這個問題。

我決定調試整個項目,發現repository-class沒有在XMLDriver中設置,它將在元數據中設置customRepositoryName。這是因爲我正在使用XML映射,對於此實體,我使用註釋提供了repositoryClass

再次感謝:)