2011-11-16 112 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