Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 实体类映射无效_Php_Symfony - Fatal编程技术网

Php 实体类映射无效

Php 实体类映射无效,php,symfony,Php,Symfony,您好,我正在尝试在页表和块表之间创建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.

您好,我正在尝试在页表和块表之间创建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.
以下是我的页面和块实体

第页:

区块:

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

我不确定它有什么问题,但似乎与注释有关。请帮忙,谢谢

在注释中,您调用属性块和页,但在实际代码中它们被称为块和页

第页:

区块:

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

好的,我找到了解决办法。问题在于名字

因此,如果我们用正常的术语来阅读,它是有意义的

一个页面可以有许多块。所以页面实体将是

/**
 * @ORM\OneToMany(targetEntity="Block", mappedBy="pages")
 */
private $blocks;
类似地,一个块属于多个页面。所以块实体将是

/**
 * @ORM\ManyToOne(targetEntity="Page", inversedBy="blocks")
 * @ORM\JoinColumn(referencedColumnName="id")
 */
private $pages;
targetEntity将始终是类名,在OneToMany情况下为单数,字段名为复数,在其他情况下也可能为复数


这就解决了问题。祝你有愉快的一天

我试过了,但效果不太好。我还编辑了post-added-JoinColumn,虽然我丢失了该列,但仍然得到了错误?块表中页面外键的字段名是什么?是的,目的是页面可以有许多块。块表中的字段名是由symfony自动生成的页面id。我没有在块实体中提到它。就个人而言,我会将多个单面命名为单数,所以$page,因为给定块只有一个页面,但是任何对你有用的东西。实际上,这并不是你所认为的那样。在块和页之间需要使用多个关系。实际上,一个块只能属于一个页面。