doushi9376 2016-09-30 09:00
浏览 48

教义一对多返回null

I'm trying to map a Milestones to a Project but when I try to reference the relation it's always returning null.

The database looks perfect, the targetEntity paths are correct and the scheme is validating by using

doctrine:scheme:validate

project.php

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

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:

$this->milestones;

Any idea what I might be doing wrong? Thank you.

  • 写回答

1条回答 默认 最新

  • duancan2539 2016-09-30 09:24
    关注

    Your owning entity definition i.e Project looks fine to me but your inversed entity i.e 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"

    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. 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”. The referencedColumnName always defaults to “id”, just as in one-to-one or many-to-one mappings.

    Make sure to update your database by running doctrine update command

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题