[英]Doctrine requires mappedBy in a OneToMany unidirectional association
When I try to make a OneToMany unidirectional association between this two entities i get this error when i try to update the database schema: 当我尝试在这两个实体之间建立OneToMany单向关联时,我在尝试更新数据库模式时遇到此错误:
$ app/console doctrine:schema:update --dump-sql
$ app / console doctrine:schema:update --dump-sql
[Doctrine\\ORM\\Mapping\\MappingException]
[学说\\ ORM \\映射\\ MappingException]
OneToMany mapping on field 'address' requires the 'mappedBy' attribute.字段'address'上的OneToMany映射需要'mappedBy'属性。
/**
* User
*
* @ORM\Table()
* @ORM\Entity
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Address")
* @ORM\JoinTable(name="users_address",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="address_id", referencedColumnName="id", unique=true)}
* )
*/
private $address;
//...
}
/**
* Address
*
* @ORM\Table()
* @ORM\Entity
*/
class Address
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
// ...
}
Why is "mappedBy" required if the association is unidirectional? 如果关联是单向的,为什么需要“mappedBy”? Thanks in advance.
提前致谢。
UPDATE: just as mentioned in the comment by @tchap an unidirectional OneToMany can be mapped with a @ManyToMany and a unique constraint on one of the join columns to enforce the onetomany cardinality. 更新:正如@tchap在评论中所提到的,单向OneToMany可以与@ManyToMany映射,并在其中一个连接列上使用唯一约束来强制执行onetomany基数。 Just as the documentation says, but it was a bit confusing for me because there is already a @OneToMay annotation.
正如文档所说,但对我来说有点混乱,因为已经有@OneToMay注释。 So I just have to change the above code to this (by only changing the @OneToMany to @ManyToMany):
所以我只需要将上面的代码改为此(只需将@OneToMany更改为@ManyToMany):
/**
* @ORM\ManyToMany(targetEntity="Address")
* @ORM\JoinTable(name="users_address",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="address_id", referencedColumnName="id", unique=true)}
* )
*/
private $address;
A OneToMany
has to be bi-directional and is always the inverse side of a relationship and if you define the inverse side you need to point at the owning side of the relationship with a mappedBy
attribute. OneToMany
必须是双向的,并且始终是关系的反面,如果定义反向边,则需要指向与mappedBy
属性的关系的拥有方。 The owning side is ManyToOne
. 拥有方是
ManyToOne
。 In your case this would look like this: 在你的情况下,这将是这样的:
In User
your association has a mappedBy="user"
attribute and points to the owning side Address
: 在
User
您的关联具有mappedBy="user"
属性并指向拥有方Address
:
/** ONE-TO-MANY BIDIRECTIONAL, INVERSE SIDE
* @var Collection
* @ORM\OneToMany(targetEntity="Address", mappedBy="user")
*/
protected $addresses;
In Address
your association has a inversedBy="addresses"
attribute and points to the inverse side User
: 在
Address
您的关联具有inversedBy="addresses"
属性并指向反向User
:
/** MANY-TO-ONE BIDIRECTIONAL, OWNING SIDE
* @var User
* @ORM\ManyToOne(targetEntity="User", inversedBy="addresses")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
The advantage of this solution is that you can find which user owns the Address
by doing: $address->getUser();
此解决方案的优点是您可以通过执行以下操作找到哪个用户拥有该
Address
: $address->getUser();
and that you skip adding an additional join table to your database. 并且您跳过向数据库添加其他连接表。
If you want the relationship to be uni-directional you can do as you did in your update; 如果您希望关系是单向的,您可以像在更新中那样做; define a
ManyToMany
relationship with a join table and add a unique constraint on the address_id
column. 使用连接表定义
ManyToMany
关系,并在address_id
列上添加唯一约束。
/**
* @ORM\ManyToMany(targetEntity="Address")
* @ORM\JoinTable(name="user_address",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="address_id", referencedColumnName="id", unique=true)}
* )
*/
The disadvantage of this solution is that you cannot find out which User
owns the address from the Address
resource (the Address
is not aware of the User
). 此解决方案的缺点是您无法找到哪个
User
拥有Address
资源中的Address
( Address
不知道User
)。 Such example can also be found here in the Doctrine documentation chapter 5.6. 这样的例子也可以在Doctrine文档第5.6章中找到。 One-To-Many, Unidirectional with Join Table .
具有连接表的一对多,单向 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.