简体   繁体   English

Symfony2映射适用于一个实体,但不适用于另一实体

[英]Symfony2 mapping works for one entity but not the other

I have two Entitites mapped together. 我有两个实体映射在一起。

Skin.php : Skin.php:

/**
 * @var CmsElement
 *
 * @ORM\ManyToOne(targetEntity="CmsElement")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="homepage_id", referencedColumnName="id")
 * })
 */
private $homepage;

CmsElement.php : CmsElement.php:

/**
 * @var Skin
 *
 * @ORM\ManyToOne(targetEntity="Skin")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="skin_id", referencedColumnName="id")
 * })
 */
private $skinId;

And thats it. 就是这样。 My Skin table is mapped correctly, I get the id of the CmsElement. 我的皮肤表已正确映射,我得到了CmsElement的ID。 However in my Cmselement I dont get the needed skinId... It always stays NULL. 但是在我的Cmselement中,我没有获得所需的skinId ...它始终保持为NULL。 The codes are identical, why doesnt it work? 密码是相同的,为什么不起作用?

An example for better understanding: 一个更好理解的示例:

Skin: 皮肤:

id: 1
homepage_id: 2

CmsElement: CmsElement:

id: 2
skin_id: NULL

In order to set ManyToOne relation, you have to specify it in both ways like this: 为了设置ManyToOne关系,您必须以两种方式指定它:

// Skin.php
/**
 * @var CmsElement
 *
 * @ORM\OneToMany(targetEntity="CmsElement", mappedBy="skinId")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="homepage_id", referencedColumnName="id")
 * })
 */
private $homepage;

// CmsElement.php :
/**
 * @var Skin
 *
 * @ORM\ManyToOne(targetEntity="Skin", inversedBy="homepage")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="skin_id", referencedColumnName="id")
 * })
 */
private $skinId;

But, please take care of this points: 但是,请注意以下几点:

  • Use $skin instead of $skinId because you are referencing an entity, not only an id property 使用$ skin而不是$ skinId,因为您引用的是实体,而不仅仅是id属性
  • The relation ManyToOne refer to a side with multiple entity linked to a side with one entity, so please define wich is the multiple side and the single one. 关系ManyToOne是指具有多个实体的一侧链接到具有一个实体的一侧,因此请定义“是多侧”还是“单侧”。 I assumed that CmsElement was the multiple side so I used ManyToOne on CmsElement and OneToMany to other. 我以为CmsElement是多面的,所以我在CmsElement上使用了ManyToOne,在另一个上使用了OneToMany。 But if I was wrong, please invert this. 但是,如果我错了,请反过来。
  • Use a plurial name for attribute at the multiple side. 在多面使用属性的多名称。 For instance if you set One $skin for Many $homepages, be sure tu use the final "S", it's easier to understand. 例如,如果您为“多个$主页”设置“一个$皮肤”,请确保tu使用最后的“ S”,这样更容易理解。

Here you can find informations in order to do your relation in the right way: Sf2 Doc 在这里,您可以找到信息,以便以正确的方式进行联系: Sf2文档

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

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