使用Symfony 2.7和Doctrine 2.5,我有
Alsciende\MyBundle\Model\CycleInterface
Alsciende\MyBundle\Entity\Cycle
AppBundle\Entity\Cycle
resolve_target_entities
的doctrine orm配置,将接口映射到最终类这个系统运行良好,我能够在AppBundle中创建数据库并实现一些CRUD,直接操作目标实体。
但是,我现在想通过Interface操纵MyBundle中的目标实体。我需要获得它的存储库:
$this->getDoctrine()->getRepository('Alsciende\MyBundle\Model\CycleInterface');
但我得到了例外
class 'Alsciende\MyBundle\Model\CycleInterface' does not exist
如何获取目标实体的存储库?也就是说,如何直接调用ResolveTargetEntityListener来获取实现接口的实体的名称?
编辑:
为什么我需要那个?很简单,例如,我需要一个显示所有Cycles列表的控制器。接口定义每个Cycle都有一个id和一个名字。我想显示每个Cycle的名称和ID。为此,我需要访问实际Cycle实体的存储库。
Alsciende / MyBundle /型号/ CycleInterface.php
<?php
namespace Alsciende\MyBundle\Model;
interface CycleInterface
{
public function getId();
public function getName();
}
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
}
}
它与FosUserBundle能够管理用户实体的方式相同,即使FosUserBundle中定义的User类是抽象类。
答案 0 :(得分:-1)
如何获取目标实体的存储库?
在app/config/config.yml
put:
doctrine:
orm:
resolve_target_entities:
Namespace\InterfaceInterface: Namespace\Entity\TargetEntityImplementing
<强> BUT 强>
为什么我需要那个?很简单,例如,我需要一个显示所有Cycles列表的控制器。接口定义每个Cycle都有一个id和一个名字。我想显示每个Cycle的名称和ID。为此,我需要访问实际Cycle实体的存储库。
在这种情况下,这不是解决方案,IMO。我宁愿使用@DiscriminatorColumn
配置的实体:
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance
父类将是您正在寻找的某种界面。
我建议你在上面“合并”:创建一个实现这样一个接口的父类,然后将这个接口映射到这个类。