Symfony 2 / Doctrine 2:同一个表有两个实体,使用一个优先于另一个


Symfony 2 / Doctrine 2: Two Entities for the same table, use one in favour of the other

在我的Symfony2应用程序中,我将大多数实体提取到我使用composer安装的单独库中。

这个库不依赖Symfony2(但确实依赖Doctrine,因为我使用注释),因为我也想在其他非Symfony2项目中使用它。

库中包含一个ClientUser实体,它映射到一个client_users表。在我的Symfony2应用程序中,我想使用相同的ClientUser实体进行身份验证。这需要我实现Symfony'Component'Security'Core'User'UserInterface

问题是,我想有两个' symfony2不可知' 一个' symfonyaware '的ClientUser实体的实现(两者都应该映射到同一个表)。我试图从一个ClientUserAbstract实体扩展这两个类,但它没有工作。

<?php
namespace My'Library'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * @ORM'MappedSuperClass
 */
class ClientUserAbstract {
    // all my fields here
}

我的"Symfony2-agnostic"实体:

<?php
namespace My'Library'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * @ORM'Entity
 */
class ClientUser extends ClientUserAbstract {
    // nothing here, it's empty
}

我的"Symfony2-aware"实体:

<?php
namespace Vendor'Bundle'MyBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Symfony'Component'Security'Core'User'UserInterface;
/**
 * @ORM'Entity
 */
class ClientUser extends ClientUserAbstract implements UserInterface {
    // all methods that Symfony requires because of the UserInterface here:
    public function getRoles();
    public function getPassword();
    public function getSalt();
    public function getUsername();
    public function eraseCredentials();
}

我的Symfony 2应用程序现在检测到两个指向同一表的实体,并以异常失败。我要么需要告诉我的Symfony2应用程序"忽略"我的My'Library'Entity'ClientUser,或者我需要一种方法来扩展它。什么好主意吗?

以防其他人有这个问题,这里是我的评论转换成一个答案:

对于给定的实体管理器,严格来说每个表只有一个实体。您需要创建第二个实体管理器,并将其用于身份验证。

另外,当然,我喜欢做代表。