0

I have got some problems in mapping of Many-To-Many that implemented like one-many-one. I have read a great solution Doctrine 2 and Many-to-many link table with an extra field but there are relations between two entities through one helper. I had to rework this solution:

    /**
     * @ORM\ManyToMany(targetEntity="Person", inversedBy="marriagesInverse", cascade={"persist"})
     * @ORM\JoinTable(name="Marriages",
     * joinColumns={@ORM\JoinColumn(name="person_1", referencedColumnName="id")},
     * inverseJoinColumns={@ORM\JoinColumn(name="person_2", referencedColumnName="id")}
     * )
     */
    private $marriages;

So I got new person entity:

/**
 * Person
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Person
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Marriages", mappedBy="Person", cascade={"persist"})
     */
    private $marriages;
}

And marriages entity:

/**
 * Marriages
 *
 * @ORM\Table(name="Marriages")
 * @ORM\Entity
 */
class Marriages
{

    /** 
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="Person", inversedBy="marriages") 
     * @ORM\JoinColumn(name="person_1", referencedColumnName="id", nullable=false) 
     */
    protected $person_1;

    /** 
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="Person", inversedBy="marriages") 
     * @ORM\JoinColumn(name="person_2", referencedColumnName="id", nullable=false) 
     */
    protected $person_2;


    /**
     * @var string
     *
     * @ORM\Column(name="marriage_date", type="text", nullable=true)
     */
    private $marriage_date;
}

How would I map Person to Person through marriages in a correct way to get results like ManyToMany. And how I can get this extra field marriage_date in twig?

Community
  • 1
  • 1
Asgu
  • 712
  • 5
  • 15
  • What do you mean? You can simply use ->getMarriages() from one person to get all Marriages (so other persons) and, also, the marriage dates. This should be already a working solution (of course you're missing all getters and setters in this example) – DonCallisto Apr 02 '15 at 07:21
  • When a had ManyToMany I used $->getMarriages() and had no any problems, but now i need to add extra field marriages_date. So Marriages becomes new entity, and i need to use OneToManyToOne relations and i have mapping issues to do this. (getters and setters are removed to reduce code length in post) – Asgu Apr 02 '15 at 07:32
  • I told you: keep using ->getMarriages() and you will obtain a "Marriage" object. With this object you can get persons (getPerson1(), getPerson2()) and marriage date (getMarriageDate()). What's the big deal with this? – DonCallisto Apr 02 '15 at 07:41

0 Answers0