How to setup a many to many form in Symfony2

前端 未结 2 533
别跟我提以往
别跟我提以往 2021-02-09 08:27

I have three entities, ChannelEntity -> MatchChannelEntity <- MatchEntity, the MatchChannelEntity saves the many to many relations between the other two tables, I want a form

2条回答
  •  生来不讨喜
    2021-02-09 09:09

    Ok, I will close this question. That's because I set up the many to many relation between the two tables wrong, and the correct way is as following(I trimed the code a bit):

    Many to many: One match has many channels and one channel has many matches.

    Match:

    class Match
    {
        /**
         * @ORM\ManyToMany(targetEntity="Channel", inversedBy="matches")
         * @ORM\JoinTable(name="match_channels")
         */
        private $channels;
    

    Channel:

    class Channel
    {
        /**
         * @ORM\ManyToMany(targetEntity="Match", mappedBy="channels")
         */
        private $matches;    
    

    Doctrine will automatically create the cross reference table for you, named MatchChannels. Note the JoinTable annonation, it is very important.

    And when you done, you can create a many to many form easily, just like you create other type of forms/fields.

提交回复
热议问题