symfony2 中的自定义存储库类 [英] custom repository class in symfony2

查看:30
本文介绍了symfony2 中的自定义存储库类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 symfony2 的新手.当我通过命令行创建实体类时,我创建了一个存储库类.但我无法访问该存储库类中的自定义函数.如何在 symfony2 中创建自定义存储库类?有人可以用一些示例代码从头开始一步一步地解释吗?

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?

下面是我的存储库类

namespace MyprosymBundleEntity;

use DoctrineORMEntityRepository;

/**
 * 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();

我收到以下错误

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.

推荐答案

我想您只是忘记在您的实体中注册此存储库.您只需在实体配置文件中添加存储库类.

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.

在 src/Mypro/symBundle/Resources/config/doctrine/Register.orm.yml:

In src/Mypro/symBundle/Resources/config/doctrine/Register.orm.yml:

MyprosymBundleEntityRegister:
    type: entity
    repositoryClass: MyprosymBundleEntityRegisterRepository

不要忘记在此更改后清除缓存,以防万一.

Don't forget to clear your cache after this change, just in case.

如果您使用的是注解(而不是 yml 配置),则添加如下内容而不是上述内容:

And if you're using Annotations (instead of yml config) then instead of the above, add something like:

/**
 * @ORMEntity(repositoryClass="MyprosymBundleEntityRegisterRepository")
*/

到您的实体类以注册存储库

to your Entity class to register the repository

这篇关于symfony2 中的自定义存储库类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆