How to setup a many to many form in Symfony2

2019-03-20 03: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 to list all the channels using checkboxes, and if a match has one the of channels, the checkbox of that channel is selected, how can I do this ?

Here is the Form type code:

class MatchhType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('channels', 'entity', array('label' => 'Channels', 
                                          'class'         => 'Mikay\MikiBundle\Entity\Channel',
                                          'multiple'      => true,
                                          'expanded'      => true,
                                          'query_builder' => function ($repository) 
                                          { 
                                            return $repository->createQueryBuilder('c')->orderBy('c.name', 'ASC'); 
                                          },))

The MatchChannel type:

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

    /**
     * @var integer $match_id
     * @ORM\ManyToOne(targetEntity="Matchh", inversedBy="channels")
     * @ORM\JoinColumn(name="match_id", referencedColumnName="id", nullable="true")
     */
     private $match;

    /**
     * @var integer $channel_id
     *
     * @ORM\ManyToOne(targetEntity="Channel", inversedBy="mathces")
     * @ORM\JoinColumn(name="channel_id", referencedColumnName="id", nullable="true")
     */
   private $channel;

I will use an example to explain, say, I have three channels: channel A, channel B and channel C, and one match: match M, the match M has one channel A, this relation is saved in the match_channel table, I want a match form to show all the channels, and channel A is checked because it is owned by match M, others stay unchecked

2条回答
Summer. ? 凉城
2楼-- · 2019-03-20 03:52

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

查看更多
该账号已被封号
3楼-- · 2019-03-20 04:10

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.

查看更多
登录 后发表回答