2011-11-16 183 views
31

我是symfony2中的新成員。當我通過命令行創建實體類時創建了存儲庫類。但我無法訪問該存儲庫類中的自定義函數。如何在symfony2中創建自定義存儲庫類?有人可以從頭開始一步一步解釋一些示例代碼嗎?symfony2中的自定義存儲庫類

下面是我的倉庫類

namespace Mypro\symBundle\Entity; 

use Doctrine\ORM\EntityRepository; 

/** 
* RegisterRepository 
* This class was generated by the Doctrine ORM. Add your own custom 
* repository methods below. 
*/ 
class RegisterRepository extends EntityRepository 
{ 


    public function findAllOrderedByName() 
    { 
     return $this->getEntityManager() 
      ->createQuery('SELECT p FROM symBundle:Register p ORDER BY p.name ASC') 
      ->getResult(); 
    } 


} 

我在我的控制器被稱爲像這樣

$em = $this->getDoctrine()->getEntityManager(); 
      $pro = $em->getRepository('symBundle:Register') 
      ->findAllOrderedByName(); 

我得到了下面的錯誤

Undefined method 'findAllOrderedByName'. The method name must start with either findBy or findOneBy! 

我是否有任何錯誤我代碼?創建存儲庫類時出現任何錯誤?我是否需要使用任何課程?

回答

68

我想你只是忘了在你的實體中註冊這個倉庫。 您只需在實體配置文件中添加存儲庫類。

在SRC/Mypro/symBundle /資源/配置/教義/ Register.orm.yml:

Mypro\symBundle\Entity\Register: 
    type: entity 
    repositoryClass: Mypro\symBundle\Entity\RegisterRepository 

不要忘了此更改後清除緩存,以防萬一。

如果你正在使用註釋(而不是陽明配置),然後,而不是上面,添加類似:

/** 
* @ORM\Entity(repositoryClass="Mypro\symBundle\Entity\RegisterRepository") 
*/ 

到實體類註冊庫

+0

魯文,我得到了正確的輸出,但在我的NetBeans IDE中無法檢測到我的自定義庫function.y?你知道嗎? –

+0

我不認爲netbean可能知道自定義存儲庫。我的版本的netbean不會自動完成symfony特定的代碼。 – Reuven

+0

我有同樣的問題,我沒有用Symfony生成EntityRepository ...但是我沒有在src/Projet/Bundle/Resources/config /中有任何名爲「doctrine」的文件夾...出了什麼問題? – httpete

6

該手冊有一個漂亮的逐步指南... http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes

  • 首先定義在註釋/ YAML配置庫類
  • 創建類
  • 創建功能
  • 然後調用新創建的函數....
+0

除非您將批註從@ORM \ Entity(repositoryClass =「Acme \ StoreBundle \ Repository \ ProductRepository」)更改爲'@ORM \ Entity(repositoryClass =「)之類的東西,否則來自Manaul的示例不適用於Symfony 2.1.1。 Acme \ StoreBundle \ Entity \ ProductRepository「)'以Entity \ Product.php文件的命名空間開頭 – yitwail

2

首先,你不需要定製回購做的。您可以通過設置順序子句中的EM getRepository findBy方法:

//$em - entity manager 
//from Doctrine documentation: findBy(criteria(array), order(array), limit, offset) 
$result = $em->getRepository('symBundle:Register')->findBy(array(), array('name' => 'ASC')) 
4

當我陷入這種混亂的配置時,我也失去了很多時間。我在我的實體中將庫類配置爲註釋映射。該映射在那裏,但仍然存儲庫沒有與實體關聯。當我將註釋映射(即 @ORM \ Entity(repositoryClass =「Acme \ DemoBundle \ Entity \ UserRepository」))移動到最後一行時,它就起作用了。

/* 
* User 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository") 
*/ 
class User 
{ 
... 
}` 
+0

奇怪的是,在花費最近2個小時試圖解決問題的原因之後,它也適用於我。 –

2

我只是遵循本機文檔doc並查找我選擇的註釋。例如,我選擇yaml,將配置添加到Product.orm.yml和Category.orm。YML文件,還可以添加新的PHP屬性實體方法:

protected $products; 

public function __construct() 
{ 
    $this->products = new ArrayCollection(); 
} 

爲Category.php和

protected $category; 

爲Product.php

然後運行php app/console doctrine:generate:entities Acme ,成功地添加新的getter和setter

4

xml映射: 更新文件夾Resource/config/doctrine中的xml映射文件,ad d庫級屬性:

<entity name="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContent" table="uv_update_page_content" repository-class="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContentRepository"> 

http://doctrine-mongodb-odm.readthedocs.org/en/latest/cookbook/mapping-classes-to-orm-and-odm.html 然後更新緩存:

php app/console doctrine:cache:clear-metadata 
php app/console cache:clear