原则2:自定义存储库和继承 [英] Doctrine 2 : Custom repositories and inheritance

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

问题描述

你好,堆满了花,

使用Doctrine 2,我在自定义存储库和继承方面遇到了一些麻烦.

Working with Doctrine 2, I encounter some troubles regarding custom repositories and inheritance.

长话短说,我想做一个这样的结构:

Long story short, I want to make that kind of structure :

  • BaseEntityRepository:包含基于被叫类名的通用方法,例如findByXXX()
  • SomeEntityRepository:包含与实体类型相关的特定方法

这些类的代码如下:

BaseEntityRepository:

BaseEntityRepository :

namespace model\repositories;

use \Doctrine\ORM\EntityRepository;

class BaseEntityRepository extends EntityRepository {
    public function findByID($id) {
        $result = null;

        try {
            $dql = "SELECT a FROM " . get_called_class() . " a WHERE a.id = :id";
            $query = $this->_em->createQuery($dql);
            $query->setParameter("id", $id);
            $result = $query->getSingleResult();
        } catch (\Exception $ex) {
            echo $ex->getMessage();
        }

        return $result;
    }
}

SomeEntityRepository:

SomeEntityRepository :

namespace model\repositories;

class SomeEntityRepository extends BaseEntityRepository {

}

我的测试示例代码:

$repo = $em->getRepository("model\\entities\\SomeEntity");
$result = $repo->findByID($id);

有了这个代码,我希望 $ repo 可以通过继承 BaseEntityRepository 来访问 findByID($ id)方法.当然, SomeEntity repositoryClass 注释以 SomeEntityRepository 为目标.相反, BaseEntityRepository 并未作为独立的存储库类定位.

With this code, I expect $repo to have access to the findByID($id) method by inheritance from BaseEntityRepository. Of course, the repositoryClass annotation for SomeEntity targets SomeEntityRepository. On the contrary, BaseEntityRepository is NOT targeted as a standalone repository class.

问题是,我得到的只是一个例外:

The thing is, all I get is an exception :

的类模型\存储库\ SomeEntityRepository"子类"model \ repositories \ BaseEntityRepository"不是有效的实体,或者映射的超类.

Class "model\repositories\SomeEntityRepository" sub class of "model\repositories\BaseEntityRepository" is not a valid entity or mapped super class.

为了我自己,我无法弄清楚这段代码有什么问题.也许我错过了一些东西,但是我对存储库继承的研究并没有带来令人满意的结果.我想对存储库有某种限制,例如它们必须正常运行,所有存储库都至少应以一次一次或类似的目标作为repositoryClass,但这并不令我满意.

I can't, for the sake of me, figure out what's wrong in this code. Maybe I missed something but my research towards inheritance in repositories did not bring any satisfying results. I guess there is some kind of restriction on repositories such as for them to work properly, all should be targeted as repositoryClass at least once or something like that, but that doesn't please me.

如果您有任何见解,建议或解决方案,我将无所不在!再次感谢.

If you have any kind of insight, piece of advice or bits of solution, I'm all ears! Thanks again.

推荐答案

问题出在get_Called_class上,它不会做您想要的事情.

The problem lies with get_called_class which is not going to do what you want.

使用原则查询生成器.这样就无需了解特定的实体类.

Use the doctrine query builder. That eliminates the need to know the specific entity class.

搜索:使用Doctrine的查询生成器查询对象

Search for: Querying for Objects Using Doctrine's Query Builder

http://symfony.com/doc/current/book/doctrine.html

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

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