Search code examples
phpsymfonydoctrine-ormmany-to-many

Failed to set up a ManyToMany


I want to be able to select a school (that has its own entity) while creating a mission (also has its entity)

Since a school can have several missions, and you can select several schools at the mission's creation, I used a ManyToMany.

The problem is that after creating this "ManyToMany", generating the entities and updating my schema, Symfony created a table, but left it totally empty, without the two columns that I asked for. I'm not really used to Symfony nor to the ManyToMany system, so I might have done some mistake without noticing it, still I find this weird.

Here's the interesting part of my ecole (school) entity:

class Ecole{

// ...

    /**
     * @ORM\ManyToMany(targetEntity="MissionBundle\Entity\Mission", mappedBy="ecolesDispo")
     */
    protected $missionsDispos;

// ...

/**
 * Add missionsDispo
 *
 * @param \MissionBundle\Entity\Mission $missionsDispo
 *
 * @return Ecole
 */
public function addMissionsDispo(\MissionBundle\Entity\Mission $missionsDispo)
{
    $this->missionsDispos[] = $missionsDispo;

    return $this;
}

/**
 * Remove missionsDispo
 *
 * @param \MissionBundle\Entity\Mission $missionsDispo
 */
public function removeMissionsDispo(\MissionBundle\Entity\Mission $missionsDispo)
{
    $this->missionsDispos->removeElement($missionsDispo);
}

/**
 * Get missionsDispos
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getMissionsDispos()
{
    return $this->missionsDispos;
}

And here is the interesting part of my mission entity:

/**
 * @ORM\ManyToMany(targetEntity="EcoleBundle\Entity\Ecole", inversedBy="missionsDispo")
 * @ORM\JoinTable(name="Mission2Ecole",
 *     joinColumns={@ORM\JoinColumn(name="em_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="me_id", referencedColumnName="id")}
 *     )
 */
protected $ecolesDispo;

// ...

/**
 * Constructor
 */
public function __construct()
{
    $this->ecolesDispo = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add ecolesDispo
 *
 * @param \EcoleBundle\Entity\Ecole $ecolesDispo
 *
 * @return Mission
 */
public function addEcolesDispo(\EcoleBundle\Entity\Ecole $ecolesDispo)
{
    $this->ecolesDispo[] = $ecolesDispo;

    return $this;
}

/**
 * Remove ecolesDispo
 *
 * @param \EcoleBundle\Entity\Ecole $ecolesDispo
 */
public function removeEcolesDispo(\EcoleBundle\Entity\Ecole $ecolesDispo)
{
    $this->ecolesDispo->removeElement($ecolesDispo);
}

/**
 * Get ecolesDispo
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getEcolesDispo()
{
    return $this->ecolesDispo;
}

After all this was created, I was supposed to get a multi selector with the list of all the schools saved in the database (I already added it to the missionType file), but I get absolutely nothing.

I don't really know if I inverted the annotations, or if the "joinTable" part is correct, but I'm completely lost here.

Does anyone have an idea?

Thank you in advance


Solution

  • Just wrong typo "s"? inversedBy="missionsDispo" >>> inversedBy="missionsDispos"

    PS. Official doc here http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional