从Doctrine 2.5中的Abstract类获取目标实体的存储库

Als*_*nde 6 symfony doctrine-orm

使用Symfony 2.7和Doctrine 2.5,我有

  • 一个接口 Alsciende\MyBundle\Model\CycleInterface
  • Alsciende\MyBundle\Entity\Cycle实现接口的抽象类
  • 最后一个类AppBundle\Entity\Cycle,它扩展了抽象类并实现了接口
  • 一个doctrine orm配置,resolve_target_entities用于将接口映射到最终类

这个系统运行良好,我能够创建数据库并在AppBundle中实现一些CRUD,直接操作目标实体.

但是,我现在想通过接口操作MyBundle中的目标实体.我需要获取其存储库:

$this->getDoctrine()->getRepository('Alsciende\MyBundle\Model\CycleInterface');
Run Code Online (Sandbox Code Playgroud)

但我得到了例外

class 'Alsciende\MyBundle\Model\CycleInterface' does not exist
Run Code Online (Sandbox Code Playgroud)

如何获取目标实体的存储库?也就是说,如何直接调用ResolveTargetEntityListener来获取实现接口的实体的名称?

编辑:

我为什么需要那个?很简单,例如,我需要一个显示所有Cycles列表的控制器.接口定义每个Cycle都有一个id和一个名字.我想显示每个Cycle的名称和ID.为此,我需要访问实际Cycle实体的存储库.

Alsciende/MyBundle /型号/ CycleInterface.php

<?php 

namespace Alsciende\MyBundle\Model;

interface CycleInterface 
{
    public function getId();
    public function getName();
}
Run Code Online (Sandbox Code Playgroud)

Alsciende/MyBundle /控制器/ CycleController.php

<?php

namespace Alsciende\MyBundle\Controller;

class CycleController extends Controller
{
    public function indexAction()
    {
        $cycles = $this
            ->getDoctrine()
            ->getRepository('Alsciende\MyBundle\Model\CycleInterface')
            ->findAll();

        // return template with list $cycles
        // using only id and name properties
    }
}
Run Code Online (Sandbox Code Playgroud)

这与FosUserBundle能够管理用户实体的方式相同,即使FosUserBundle中定义的User类是抽象类.

eRI*_*RIZ -1

如何获取目标实体的存储库?

输入app/config/config.yml

doctrine: orm: resolve_target_entities: Namespace\InterfaceInterface: Namespace\Entity\TargetEntityImplementing

为什么我需要那个?很简单,例如,我需要一个显示所有周期列表的控制器。该接口定义每个 Cycle 有一个 id 和一个名称。我想显示每个 Cycle 的名称和 ID。为此,我需要访问实际 Cycle 实体的存储库。

IMO,这不是这种情况下的解决方案。我宁愿使用@DiscriminatorColumn配置好的实体: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance

父类将是您正在寻找的某种接口。

我建议您“合并”上面的内容:创建一个将实现此类接口的父类,然后将此接口映射到此类。