简体   繁体   English

Symfony 2 / Doctrine 2:同一个表的两个实体,使用一个赞成另一个

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

In my Symfony2 application I extracted most of my Entities into a separate library which I install using composer. 在我的Symfony2应用程序中,我将大部分实体提取到一个单独的库中,我使用composer进行安装。

This library does not have a dependency on Symfony2 (but does depend on Doctrine because I use annotations), because I would like to use it in other non-Symfony2 projects as well. 这个库没有依赖于Symfony2(但是依赖于Doctrine,因为我使用了注释),因为我想在其他非Symfony2项目中使用它。

The library contains a ClientUser Entity, which maps to a client_users table. 该库包含ClientUser实体,该实体映射到client_users表。 In my Symfony2 application, I would like to use the same ClientUser Entity for authentication. 在我的Symfony2应用程序中,我想使用相同的ClientUser实体进行身份验证。 This requires me to implement Symfony\\Component\\Security\\Core\\User\\UserInterface . 这需要我实现Symfony\\Component\\Security\\Core\\User\\UserInterface

The problem is that I would like to have both a 'Symfony2-agnostic' and a 'Symfony-aware' implementation of a ClientUser Entity (which both should map to the same table). 问题是我希望同时具有'Symfony2-agnostic' 'Symfony-aware'实现ClientUser实体(两者都应该映射到同一个表)。 I tried to extend both classes from a ClientUserAbstract Entity, but it didn't work. 我试图从ClientUserAbstract实体扩展这两个类,但它没有用。

<?php
namespace My\Library\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\MappedSuperClass
 */
class ClientUserAbstract {

    // all my fields here
}

My "Symfony2-agnostic" Entity: 我的“Symfony2-agnostic”实体:

<?php
namespace My\Library\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class ClientUser extends ClientUserAbstract {

    // nothing here, it's empty
}

My "Symfony2-aware" Entity: 我的“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();
}

My Symfony 2 app now detects two Entities which point to the same table and fails with an Exception. 我的Symfony 2应用程序现在检测到两个指向同一个表的实体,并以异常方式失败。 I either need to tell my Symfony2 app to "ignore" my My\\Library\\Entity\\ClientUser , or I need a way to extend it. 我要么告诉我的Symfony2应用程序“忽略”我的My\\Library\\Entity\\ClientUser ,要么我需要一种方法来扩展它。 Any ideas? 有任何想法吗?

Just in case anyone else has this issue, here is my comment converted to an answer: 以防万一其他人有这个问题,这里是我的评论转换为答案:

For a given entity manager, it's strictly one entity per table. 对于给定的实体管理器,每个表严格一个实体。 You need to create a second entity manager and use it for authentication. 您需要创建第二个实体管理器并将其用于身份验证。

Plus of course, I like getting reps. 当然,我喜欢得到代表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM