如何使用Doctrine注释来更改实体子类中的列名? [英] How can I use Doctrine Annotations to change a column name in an entity subclass?

查看:88
本文介绍了如何使用Doctrine注释来更改实体子类中的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FOSUserBundle与一个新的Symfony项目必须使用现有的架构。我的用户实体按说明说明扩展了FOS\UserBundle\Entity\User,但电子邮件列名称为email_addr而不是电子邮件。由于父代定义$ email,我无法重新声明它附加我的注释。

  / ** 
* @var string $ emailAddr
*
* @ ORM\Column(name =email_addr,type =text,nullable = false)
* /
protected $电子邮件;

我得到的例外是:

  [Doctrine\ORM\Mapping\MappingException] 
Foo\DataBundle\Entity\User中的属性email已被声明,但必须是只有一次

我的问题是:




  • Doctrine2子类如何使用注释来更改父项中定义的内容?

  • 或者如何覆盖FOSUserBundle中的列名?


解决方案

我找到了一个答案:



更多关于教义实现


如果需要更改映射(例如,将字段名称修改为al egacy数据库),唯一的解决方案是再次编写整个映射,而不会继承映射超类的映射。在这种情况下,您的实体应直接从FOS\UserBundle\Model\User(以及该组的FOS\UserBundle\Model\Group)扩展。



I'm using FOSUserBundle with a new Symfony project that has to work with an existing schema. My user entity extends FOS\UserBundle\Entity\User as the instructions say, but the email column is named "email_addr" instead of "email". Since the parent defines $email I can't re-declare it to attach my annotation.

 /**
  * @var string $emailAddr
  *
  * @ORM\Column(name="email_addr", type="text", nullable=false)
  */
 protected $email;

The exception I get is:

[Doctrine\ORM\Mapping\MappingException]                                                                                
Property "email" in "Foo\DataBundle\Entity\User" was already declared, but it must be declared only once  

My question is either:

  • How can a Doctrine2 subclass use an annotation to alter something defined in the parent?
  • Or how can I override column names in the FOSUserBundle?

解决方案

I found an answer:

More about Doctrine implementations

If you need to change the mapping (for instance to adapt the field names to a legacy database), the only solution is to write the whole mapping again without inheriting the mapping from the mapped superclass. In such case, your entity should extend directly from FOS\UserBundle\Model\User (and FOS\UserBundle\Model\Group for the group).

这篇关于如何使用Doctrine注释来更改实体子类中的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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