简体   繁体   中英

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:

[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).

That resolves the issue. Have a good day !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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