Skip to content

ManyToMany inverse relationships need by_reference on form to update #1566

Closed
@andrewyager

Description

@andrewyager

As per discussion on #symfony this evening, it should be documented somewhere that when you have a ManyToMany relationship, and you need the inverse to update the ORM model, you need to pass the by_reference option.

https://gist.github.com/3121916 is a good example of setting up this sort of relationship. Then in your form:

<?php

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserGroupType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('users', null, array('label'=>'Users', 'by_reference' => false))
...
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'UserGroup'
        ));
    }

    public function getName()
    {
        ...
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FormactionableClear and specific issues ready for anyone to take them.good first issueIdeal for your first contribution! (some Symfony experience may be required)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions