Symfony 2 /教义2:同一张桌子的两个实体,使用一个赞成对方 [英] Symfony 2 / Doctrine 2: Two Entities for the same table, use one in favour of the other

查看:131
本文介绍了Symfony 2 /教义2:同一张桌子的两个实体,使用一个赞成对方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Symfony2应用程序中,我将大部分实体提取到我使用作曲家安装的单独的库中。

In my Symfony2 application I extracted most of my Entities into a separate library which I install using composer.

这个库没有依赖于Symfony2(但是依赖于Doctrine,因为我使用注释),因为我想在其他非Symfony2项目中使用它好。

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.

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

The library contains a ClientUser Entity, which maps to a client_users table. In my Symfony2 application, I would like to use the same ClientUser Entity for authentication. This requires me to implement Symfony\Component\Security\Core\User\UserInterface.

问题是我想同时使用Symfony2-agnostic和Symfony-aware实现 ClientUser 实体(它们都应映射到同一个表)。我试图从ClientUserAbstract实体扩展两个类,但是它没有起作用。

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). I tried to extend both classes from a ClientUserAbstract Entity, but it didn't work.

<?php
namespace My\Library\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    // all my fields here
}

我的Symfony2-agnostic实体:

My "Symfony2-agnostic" Entity:

<?php
namespace My\Library\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    // nothing here, it's empty
}

我的Symfony2感知实体:

My "Symfony2-aware" Entity:

<?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 ,或者我需要一种方法来扩展它。任何想法?

My Symfony 2 app now detects two Entities which point to the same table and fails with an Exception. I either need to tell my Symfony2 app to "ignore" my My\Library\Entity\ClientUser, or I need a way to extend it. 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.

这篇关于Symfony 2 /教义2:同一张桌子的两个实体,使用一个赞成对方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆