php symfony2 中的自定义存储库类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8146461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
custom repository class in symfony2
提问by Asish AP
I'm new in symfony2.I created a repository class when I created an entity class through command line.But I couldn't access my custom functions in that repository class. how to create custom repository class in symfony2? Can anybody give me a step by step explanation from scratch with some sample code?
我是 symfony2 的新手。当我通过命令行创建实体类时,我创建了一个存储库类。但是我无法访问该存储库类中的自定义函数。如何在 symfony2 中创建自定义存储库类?任何人都可以用一些示例代码从头开始一步一步地解释吗?
Below is my repository class
下面是我的存储库类
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();
}
}
I called in my controller like this way
我这样叫我的控制器
$em = $this->getDoctrine()->getEntityManager();
$pro = $em->getRepository('symBundle:Register')
->findAllOrderedByName();
I got the below error
我收到以下错误
Undefined method 'findAllOrderedByName'. The method name must start with either findBy or findOneBy!
Do I have any mistake in my code ? Any mistake in creating repository class? did i need to use any class.
我的代码有错误吗?创建存储库类有什么错误吗?我需要使用任何类吗?
回答by Reuven
I think you just forgot to register this repository in your entity. You just have to add in your entity configuration file the repository class.
我认为您只是忘记在您的实体中注册此存储库。您只需在实体配置文件中添加存储库类。
In src/Mypro/symBundle/Resources/config/doctrine/Register.orm.yml:
在 src/Mypro/symBundle/Resources/config/doctrine/Register.orm.yml 中:
Mypro\symBundle\Entity\Register:
type: entity
repositoryClass: Mypro\symBundle\Entity\RegisterRepository
Don't forget to clear your cache after this change, just in case.
不要忘记在此更改后清除缓存,以防万一。
And if you're using Annotations (instead of yml config) then instead of the above, add something like:
如果您使用的是 Annotations(而不是 yml config),那么请添加如下内容而不是上述内容:
/**
* @ORM\Entity(repositoryClass="Mypro\symBundle\Entity\RegisterRepository")
*/
to your Entity class to register the repository
到您的实体类以注册存储库
回答by Manse
The manual has a nice step by step guide ... http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes
该手册有一个很好的分步指南...... http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes
- First define the repository class in the annotations / yaml configuration
- Create the class
- Create the function
- Then call the newly created function ....
- 首先在annotations/yaml配置中定义repository类
- 创建班级
- 创建函数
- 然后调用新创建的函数....
回答by steady_daddy
I too lost a lot of time when I got into this mess of configuration. I was configuring the repository class in my Entity as annotation mapping. The mapping was there but still the repository was not associated with the entity. When I moved the annotation mapping i.e. @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository"), to the last line, it worked.
当我陷入这种混乱的配置时,我也浪费了很多时间。我将实体中的存储库类配置为注释映射。映射在那里,但存储库仍然没有与实体关联。当我将注释映射即@ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository") 移动到最后一行时,它起作用了。
/*
* User
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository")
*/
class User
{
...
}`
回答by Peniel1127
xml for mapping: Update xml mapping file in folder Resource/config/doctrine, add repository-class attribute:
用于映射的 xml:更新文件夹 Resource/config/doctrine 中的 xml 映射文件,添加 repository-class 属性:
<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.htmlThen update cache:
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
回答by 1nstinct
I simply follow to native documentation docand look for the annotation that I selected. For example I selected yaml, added configurations to Product.orm.yml and Category.orm.yml files, also add new php attributes to Entity Methods :
我只是遵循本机文档文档并查找我选择的注释。例如我选择了 yaml,向 Product.orm.yml 和 Category.orm.yml 文件添加了配置,还向实体方法添加了新的 php 属性:
protected $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
for Category.php and
对于 Category.php 和
protected $category;
for Product.php
用于产品.php
then run php app/console doctrine:generate:entities Acme
,that successfully add new getters and setters
然后运行php app/console doctrine:generate:entities Acme
,成功添加新的 getter 和 setter
回答by thorinkor
First of all You don't need custom repo to do that.. You can set the order by clause in the EM getRepository findBy method:
首先,您不需要自定义存储库来执行此操作。您可以在 EM getRepository findBy 方法中设置 order by 子句:
//$em - entity manager
//from Doctrine documentation: findBy(criteria(array), order(array), limit, offset)
$result = $em->getRepository('symBundle:Register')->findBy(array(), array('name' => 'ASC'))