Skip to content

ManyToMany inverse relationships need by_reference on form to update #1566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andrewyager opened this issue Jul 16, 2012 · 4 comments
Closed
Labels
actionable Clear and specific issues ready for anyone to take them. Form good first issue Ideal for your first contribution! (some Symfony experience may be required)
Milestone

Comments

@andrewyager
Copy link

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()
    {
        ...
    }
}
@Sgoettschkes
Copy link
Contributor

Looking at the cookbook How to Embed a Collection of Forms, I see the by_reference option in Listing 7. Is this enough or do you think we need a more in-depth description of this?

@weaverryan
Copy link
Member

I think we should also highlight it - even with a little code tip that points to the actual by_reference docs on that page - in the entity form reference type (http://symfony.com/doc/current/reference/forms/types/entity.html).

If we do that, then I think we're in good shape. This is also related to the still-needed documentation of the addXxx and removeXxx methods in Symfony 2.1 - see #1322 and #1326.

Thanks!

@fredpeaks
Copy link

I think this problem still has to be fixed. There is no documentation of the by_reference option in entity form type but the option still exists and is needed to be able to persist manytomany from the inverse side (by_reference has to be false).

@xabbuh
Copy link
Member

xabbuh commented Feb 6, 2014

@fredpeaks This should be covered with #3410.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable Clear and specific issues ready for anyone to take them. Form good first issue Ideal for your first contribution! (some Symfony experience may be required)
Projects
None yet
Development

No branches or pull requests

5 participants