I got three classes:
- ProjectType
- Phase
- ProjectTypePhase (This is to create a seperate join table to make sure ProjectType and Phase gets linked with an id for ordering)
ProjectType
/**
* @OneToMany(targetEntity="ProjectTypePhase", mappedBy="project_type")
*/
private $projectTypePhases;
public function __construct()
{
$this->projectTypePhases = new ArrayCollection();
}
/**
* @return mixed
*/
public function getProjectTypePhases()
{
return $this->projectTypePhases;
}
Phase
/**
* @OneToMany(targetEntity="ProjectTypePhase", mappedBy="phase")
*/
private $projectTypePhases;
public function __construct()
{
$this->projectTypePhases = new ArrayCollection();
}
/**
* @return mixed
*/
public function getProjectTypePhases()
{
return $this->projectTypePhases;
}
ProjectTypePhase
/**
* @ManyToOne(targetEntity="ProjectType", inversedBy="project_type_phase")
* @JoinColumn(name="project_type_id", referencedColumnName="id")
*/
private $projectType;
/**
* @ManyToOne(targetEntity="Phase", inversedBy="project_type_phase")
* @JoinColumn(name="phase_id", referencedColumnName="id")
*/
private $phase;
public function __construct($projectType, $phase)
{
$this->projectType = $projectType;
$this->phase = $phase;
}
I filled the database through MySQL workbench since they are only id entries (correct?). Anyway, whenever I try to do $projectType->getProjectTypePhases();` it returns an empty collection. Also when I try this:
$repository = $this->getDoctrine()->getRepository('AppBundle:ProjectTypePhase');
$projectPhases = $repository->findAll();
I get all the entries, but somehow the variable name for instance of the projecttype and phase entity is null even though they are filled in the database. The corresponding keys are correct and names are filled. What goes wrong? And is there some approach that needs to be done what I am missing? The thing I am trying to accomplish is:
I had a Many to Many relationship which worked fine between ProjectType and Phase. However since there is no id for that table things didn't get in the order I intended to. I had searched for a solution and that was a seperate entity which could handle the relationship between ProjectType and Phase and add additional columns. Is this the correct way?