简体   繁体   English

实体类映射无效

[英]Entity class mapping is invalid

Hi i am trying to create a OneToMany relationship between Page and Block tables but i am getting the following error on validating the schema: 嗨,我正在尝试在Page和Block表之间创建OneToMany关系,但是在验证架构时出现以下错误:

[Mapping]  FAIL - The entity-class 'mypath\Entity\Block' mapping is invalid:
* The association mypath\Entity\Block#pages refers to the inverse side field     mypath\Entity\Page#blocks which does not exist.

[Mapping]  FAIL - The entity-class 'mypath\Entity\Page' mapping is invalid:
* The association mypath\Entity\Page#block refers to the owning side field     mypath\Entity\Block#page which does not exist.

Following are my page and Block Entities 以下是我的页面和阻止实体

Page: 页:

/**
* @ORM\OneToMany(targetEntity="Block", mappedBy="page")
*/
private $block;

Block: 块:

/**
* @ORM\ManyToOne(targetEntity="Page", inversedBy="block")
* @ORM\JoinColumn(referencedColumnName="id")
*/
private $pages;

I am not sure what wrong with it but seems to be something related to annotations. 我不确定这有什么问题,但似乎与注解有关。 Please help, Thanks !!! 请帮助,谢谢!

In the annotation, you call the properties "blocks and "page", but they're called "block" and "pages" in the actual code. 在注释中,您将属性称为“块和页面”,但在实际代码中将它们分别称为“块”和“页面”。

Page: 页:

/**
* @ORM\OneToMany(targetEntity="Block", mappedBy="page")
*/
private $blocks;

Block: 块:

/**
* @ORM\ManyToOne(targetEntity="Page", inversedBy="blocks")
*/
private $page;

Ok i found the solution. 好的,我找到了解决方案。 The problem was with the names. 问题出在名字上。

So if we read it in normal terms, it make sense. 因此,如果我们以常规术语阅读它,这是有道理的。

A page can have many blocks. 一个页面可以有很多块。 So page entity will be 因此页面实体将是

/**
 * @ORM\OneToMany(targetEntity="Block", mappedBy="pages")
 */
private $blocks;

Similarly, a block belongs to many pages. 同样,一个块属于许多页面。 So block entity will be 所以块实体将是

/**
 * @ORM\ManyToOne(targetEntity="Page", inversedBy="blocks")
 * @ORM\JoinColumn(referencedColumnName="id")
 */
private $pages;

targetEntity will always be the class name which would be singular and field names will be plural(in case of OneToMany and may be in other cases as well). targetEntity将始终是类名称,该类名称将是单数形式,而字段名称将是复数形式(在OneToMany情况下,在其他情况下也可能是)。

That resolves the issue. 这样就解决了问题。 Have a good day !!! 祝你有美好的一天 !!!

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

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