0

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?

reaanb
  • 9,806
  • 2
  • 23
  • 37
DaViDa
  • 641
  • 1
  • 8
  • 28

1 Answers1

0

Modify ProjectTypePhase as follows

/**
 * @ManyToOne(targetEntity="ProjectType", inversedBy="projectTypePhases")
 * @JoinColumn(name="project_type_id", referencedColumnName="id")
 */
private $project_type;
/**
 * @ManyToOne(targetEntity="Phase", inversedBy="projectTypePhases")
 * @JoinColumn(name="phase_id", referencedColumnName="id")
 */
private $phase;

You must make names match in order to make the things works. However it's pretty strange that doctrine does not warn you

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • Still I get empty names. I can even find by a ProjectType object. So that is all working both with your example as with mine. Just the attribute 'name' is null both on ProjectType and Phase even though they are filled in the database – DaViDa Sep 26 '16 at 08:29
  • When I do $repository->findAll($projectType); now where $projectType is an object it does give me the names of the projecttype however phase name is still empty – DaViDa Sep 26 '16 at 08:34
  • @DaViDa maybe you have typos elsewhere – DonCallisto Sep 26 '16 at 08:40
  • I did {{ phase.getPhase().getName() }} in the template and it did find the name, I think the answer has something to do with the answer of this question: http://stackoverflow.com/questions/14979123/symfony-manytoone-relationship-getter-returns-empty-object . However I still want to know why the ArrayCollections stay empty and why this happens in general – DaViDa Sep 26 '16 at 08:45