简体   繁体   English

原则一对多返回null

[英]Doctrine one-to-many returning null

I'm trying to map a Milestones to a Project but when I try to reference the relation it's always returning null. 我正在尝试将里程碑映射到项目,但是当我尝试引用该关系时,它总是返回null。

The database looks perfect, the targetEntity paths are correct and the scheme is validating by using 数据库看起来很完美,targetEntity路径正确,并且该方案通过使用进行验证

doctrine:scheme:validate

project.php project.php

/**
 * @ORM\OneToMany(targetEntity="Planning\Readmodel\Project\Milestone\Milestone", mappedBy="project", cascade={"persist", "remove"})
 */
private $milestones;

milestone.php milestone.php

/**
 * @ORM\ManyToOne(targetEntity="Planning\Readmodel\Project\Project", inversedBy="milestones", cascade={"persist", "remove"})
 * @ORM\JoinColumn(name="projectId", referencedColumnName="projectId")
 */
private $project;

But when I try to get the milestone I get null using: 但是,当我尝试获取里程碑时,我使用以下方法得到null:

$this->milestones;

Any idea what I might be doing wrong? 知道我做错了什么吗? Thank you. 谢谢。

Your owning entity definition ie Project looks fine to me but your inversed entity ie Milestone has a problem in JoinColumn annotation in JoinColumn annotation name relates to the column of your current entity which hold the relation to project entity but in referencedColumnName you have to provide the column of your parent entity that is primary key of project entity which should be referencedColumnName="id" 您拥有的实体定义(即Project对我而言很好,但您的倒置实体(即Milestone )的JoinColumn注解中存在问题JoinColumn注解name与当前实体的列相关, JoinColumn与项目实体保持关系,但在referencedColumnName您必须提供该列作为项目实体主键的父实体的名称,应referencedColumnName="id"

So your annotation for milestone entity should be like 因此,您对里程碑实体的注释应类似于

/**
 * @ORM\ManyToOne(targetEntity="Planning\Readmodel\Project\Project", inversedBy="milestones", cascade={"persist", "remove"})
 * @ORM\JoinColumn(name="project_id", referencedColumnName="id")
 */
private $project;

According to the official docs 5.11. 根据官方文档5.11。 Mapping Defaults 映射默认值

The name of the join table defaults to a combination of the simple, unqualified class names of the participating classes, separated by an underscore character. 联接表的名称默认为参与类的简单,无限定的类名的组合,并用下划线字符分隔。 The names of the join columns default to the simple, unqualified class name of the targeted class followed by “_id”. 连接列的名称默认为目标类的简单,无限制的类名,后跟“ _id”。 The referencedColumnName always defaults to “id”, just as in one-to-one or many-to-one mappings. 就像一对一或多对一映射一样,referencedColumnName始终默认为“ id”。

Make sure to update your database by running doctrine update command 确保通过运行doctrine update命令来更新数据库

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

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