0

I have a Many to Many relationship in Symfony 2. Diagram below.

https://i.stack.imgur.com/x6AYs.png

From Tema Entity i can get all Topico records related using this ORM definition

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Application\RiesgoBundle\Entity\Topico", inversedBy="tema")
 * @ORM\JoinTable(name="tema_topico",
 *   joinColumns={
 *     @ORM\JoinColumn(name="tema", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="topico", referencedColumnName="id")
 *   }
 * )
 */
private $topico;

And using this method

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

However, i don't know how to access to "impacto" and "ocurrencia" values stored on tema_topico table. Is there a way to do this using the Entity Manager?

Cœur
  • 37,241
  • 25
  • 195
  • 267
gizmo_marco
  • 675
  • 5
  • 2

1 Answers1

3

On a ManyToMany association, you cant store extra fields. To achieve a "ManyToMany" with extra fields, you will have to do a OneToMany - ManyToOne. It will give you something like.

class Tema {
    /*
     * @ORM\OneToMany(targetEntity="temaTopico", mappedBy="tema")
     */
    private $temaTopico;
}

class Topico {
    /*
     * @ORM\OneToMany(targetEntity="temaTopico", mappedBy="topico")
     */
    private $temaTopico;
}

class TemaTopico {
    /*
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="Tema", inversedBy="temaTopico")
     */
    private $tema;

    /*
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="Topico", inversedBy="temaTopico")
     */
    private $topico;

    /*
     * @ORM\Column(name="impacto", type="string")
     */
    private $impacto;
}

see this and this

Community
  • 1
  • 1
AlixB
  • 1,208
  • 2
  • 10
  • 19