无法在JoinColumn中为Symfony2/Doctrine中的外键引用定义列类型 [英] Not able to define column type in JoinColumn for a foreign key reference in Symfony2/Doctrine

查看:52
本文介绍了无法在JoinColumn中为Symfony2/Doctrine中的外键引用定义列类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该是一个简单的问题,但我似乎无法解决.

This seems like it should be an easy issue but I can't seem to get it solved.

我有两个实体,人物和旗帜.Flag与Person有ManyToOne关系.我遇到问题的地方是Person的id字段是bigint,而不是类型int.

I have two Entities, Person and Flag. Flag has a ManyToOne relationship to Person. Where I'm running into an issue is that Person's id field is a bigint, not a type int.

因此,在Flag实体的ManyToOne字段上使用如下语法:

So, with syntax like this on the ManyToOne field on the Flag entity:

/**
 * @ORM\ManyToOne(targetEntity="Person", inversedBy="flags")
 * @ORM\JoinColumn(name="personId", referencedColumnName="id")
 */
protected $person;

我收到外键错误,类似

An exception occurred while executing 'ALTER TABLE flags ADD CONSTRAINT FK_B0541BAA20C4B1C FOREIGN KEY

我相信除了产生此错误外,我还用尽了所有其他可能性,因为personId字段是整数,而person的ID字段是bigint.这些外键关系在此捆绑包中的其他实体(仅是Person以及引起问题的bigint主键)之间运行良好.

I believe I've exhausted all other possibilities of this error besides it being generated because the personId field is an integer and the person's ID field is a bigint. These foreign key relationships have been working fine among other entities in this bundle, its only the Person and its bigint primary key that is causing issues.

我无法在不出现类似以下错误的情况下将type ="bigint"添加到JoinColumn:

I can't add type="bigint" to JoinColumn without getting an error like:

The annotation @ORM\JoinColumn declared on property Acme\AcmeBundle\Entity\PersonFlag::$person does not have a pro
  perty named "type"

而且,如果我添加Column注释以声明类型,它只会忽略JoinColumn,如下所示:

And, if I add Column annotation in order to declare the type, it just ignores the JoinColumn, as shown below:

/**
 * @ORM\ManyToOne(targetEntity="Person", inversedBy="flags")
 * @ORM\JoinColumn(name="personId", referencedColumnName="id")
 * @ORM\Column(type="bigint")
 */
protected $person;

这不会产生任何错误,但是会创建一个没有外键的名为"person"的列.

This doesn't generate any errors, but it creates a column named "person" that has no foreign keys.

那么,我想念的是什么,我该如何将ManyToOne关系添加到具有bigint作为主键的实体中?

So, what am I missing, how can I go about adding this ManyToOne relationship to an entity that has a bigint as it's primary key?

这是Person实体的ID字段的注释:

Here is the annotation for the Person entity's ID field:

/**
 * @ORM\Id
 * @ORM\Column(type="bigint")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

TY!

推荐答案

在实体之间的关系中,我认为您可以避免使用列类型,因为外键应该已经在模型中进行了映射(例如,ID).试试这个:

In relationships between entities I think you could avoid the column type, because the foreign keys should be already mapped in the model (for example, the IDs). Try this:

/**
 * @ORM\ManyToOne(targetEntity="Person", inversedBy="flags")
 * @ORM\JoinColumn(name="personId", referencedColumnName="id")
 */
protected $person;

然后,在您的个人模型中,您应该具有以下内容:

Then, in your person model you should have something like this:

/**
 * @OneToMany(targetEntity="Flag", mappedBy="person")
 */
protected $flags;

这篇关于无法在JoinColumn中为Symfony2/Doctrine中的外键引用定义列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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