0

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.

5
  • 1
    Where do you try to get the milestone ? Commented Sep 30, 2016 at 9:57
  • Can you post the code where you want to retrieve milestones? Are the data you want to get already in database, or are you trying to insert a new relation? Commented Sep 30, 2016 at 10:26
  • What DOZ said. Can you provide more context around the line $this->milestones; where are you using this line to call the private $milestones property? Commented Oct 5, 2016 at 23:51
  • Faced with this problem too, did you solved it? Commented Mar 2, 2020 at 12:01
  • Oh my this is way to long ago, can't even remember I ever posted this, sorry! Commented Mar 3, 2020 at 13:03

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, in both columns: projectId is used as the name of the key (not my favorite naming convention but I have to). So your proposal didn't solve it. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.