Can学说';s的resolve_target_entities用于发现存储库


Can Doctrine's resolve_target_entities be used for discovering repositories?

我在两个项目(ProjectAProjectB)上使用Symfony 2.8.3和Doctrine 2.4.8,并希望在我的捆绑包中创建一个共享服务(SharedBundle

这个服务需要在数据库上操作,所以我想我可以使用config.yml中的resolve_target_entities指令从SharedBundle访问我的项目的存储库(在ProjectA和ProjectB中定义)。

存储库如下所示:

<?php
namespace My'ProjectA'Repository;
class FooRepository extends 'Doctrine'ORM'EntityRepository
{
    // ...
}

首次尝试-接口

app/config:

doctrine:
    orm:
        # ...
        resolve_target_entities:
            My'SharedBundle'Model'FooInterface: My'ProjectA'Entity'Foo

所以我创建了一个接口:

<?php
namespace My'SharedBundle'Model;
interface FooInterface 
{
}

实体实现此接口

<?php
namespace My'ProjectA'Entity;
class Foo implements 'My'SharedBundle'Model'FooInterface
{
    // ...
}

尝试从SharedService访问存储库,如下所示:

$repository = $this->entityManager->getRepository('My'SharedBundle'Model'FooInterface::class);

这抛出:

Class 'My'SharedBundle'Model'FooInterface' does not exist

第二次尝试-抽象实体

app/config.yml

doctrine:
    orm:
        # ...
        resolve_target_entities:
            My'SharedBundle'Model'AbstractFoo: My'ProjectA'Entity'Foo

我创建了一个抽象实体

<?php
namespace My'SharedBundle'Model;
abstract class AbstractFoo
{
    // ...
}

继承:

<?php
namespace My'ProjectA'Entity;
class Foo extends AbtractFoo
{
    // ...
}

现在我想从SharedService访问存储库,如下所示:

$repository = $this->entityManager->getRepository('My'SharedBundle'Model'AbstractFoo::class);

但这抛出:

The class 'My'SharedBundle'Model'AbstractFoo' was not found in the chain configured namespaces My'ProjectA'Entity

这是否仅适用于学说关系?

DoctrineBundle的resolve_target_entities仅适用于关系。

实际上,您不需要使用它将不同的类(即在两个应用程序中)映射到同一个数据库表。

您应该只使用继承映射。

创建一个映射的超类class A(存在于两个应用程序中),该超类由class B(仅存在于应用程序#1中)和class C(仅存在于应用程序#2中)扩展。