Skip to content

Commit 84a3c77

Browse files
committed
Deprecate BelongsToMany
1 parent 110b645 commit 84a3c77

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

readme.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,16 @@ $form = $this->createNamed('user', FormType::class, $user)
163163
See http://symfony.com/doc/current/book/forms.html for more information.
164164
## BelongsToMany relations
165165

166-
BelongsToMany behaves differently, because it isn't an actual attribute on your model. Instead, we can use the custom `belongs_to_many` type to fill the Form and sync it manually.
166+
BelongsToMany behaves differently, because it isn't an actual attribute on your model. Instead, we can set the `mapped` option to `false` and sync it manually.
167167

168168
```php
169+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
170+
169171
$builder
170-
->add('users', 'belongs_to_many', [
172+
->add('users', ChoiceType::class, [
171173
'choices' => \App\User::lists('name', 'id'),
174+
'multiple' => true,
175+
'mapped' => false,
172176
'expanded' => true, // true=checkboxes, false=multi select
173177
]);
174178
```
@@ -179,13 +183,14 @@ if ($form->isSubmitted()) {
179183
$this->validate($request, $rules);
180184

181185
$item->save();
182-
$item->users()->sync($request->get('users'));
186+
$item->users()->sync($form->get('users')->getData());
183187

184188
return redirect()->back();
185189
}
186190
```
191+
See for more options the [choice type documentation](http://symfony.com/doc/current/reference/forms/types/choice.html).
187192

188-
The `belongs_to_many` type extends the [choice type](http://symfony.com/doc/current/reference/forms/types/choice.html), but is `multiple` by default.
193+
> Note: The BelongsToManyType is deprecated in favor of the ChoiceType from Symfony.
189194
190195
## Translation labels
191196

src/Extension/Eloquent/BelongsToManyType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Symfony\Component\Form\FormBuilderInterface;
66
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
77

8+
/**
9+
* @deprecated Use a 'ChoiceType' with multiple=true, mapped=false
10+
*/
811
class BelongsToManyType extends AbstractType {
912

1013
public function buildForm(FormBuilderInterface $builder, array $options)

0 commit comments

Comments
 (0)