How to setup a many to many form in Symfony2

…衆ロ難τιáo~ 提交于 2019-12-03 08:13:37

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.

I have finally found a workaround for this. You may look into the source code http://www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation-with.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!