From 5122d2c7c0cf6fe409fcaa046db75da0b7813125 Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Tue, 13 Feb 2018 23:56:09 +0100 Subject: [PATCH 1/2] Explaining the "one-to-many" case more explicitly Please double-check: I'm not certain if "For a many-to-many relationship:" (line 574) is correct! --- form/form_collections.rst | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/form/form_collections.rst b/form/form_collections.rst index ae4debaa455..396e7f40a48 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -557,7 +557,21 @@ you will learn about next!). The trick is to make sure that the single "Task" is set on each "Tag". One easy way to do this is to add some extra logic to ``addTag()``, which is called by the form type since ``by_reference`` is set to - ``false``:: + ``false``. + + For a one-to-many relationship you just need to add one line to ``Task``: + + // src/AppBundle/Entity/Task.php + + // ... + public function addTag(Tag $tag) + { + $tag->setTask($this); + + $this->tags->add($tag); + } + + For a many-to-many relationship: // src/AppBundle/Entity/Task.php @@ -581,9 +595,6 @@ you will learn about next!). } } - If you have a one-to-many relationship, then the workaround is similar, - except that you can simply call ``setTask()`` from inside ``addTag()``. - .. _form-collections-remove: Allowing Tags to be Removed From fdba8ae49b0f74f3df044c42a3ed07ed074e4ea9 Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Fri, 23 Feb 2018 18:10:07 +0100 Subject: [PATCH 2/2] As suggested... See https://github.com/symfony/symfony-docs/pull/9259#discussion_r170283369 --- form/form_collections.rst | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/form/form_collections.rst b/form/form_collections.rst index 396e7f40a48..4a446121dc0 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -557,33 +557,23 @@ you will learn about next!). The trick is to make sure that the single "Task" is set on each "Tag". One easy way to do this is to add some extra logic to ``addTag()``, which is called by the form type since ``by_reference`` is set to - ``false``. - - For a one-to-many relationship you just need to add one line to ``Task``: + ``false``:: // src/AppBundle/Entity/Task.php - // ... - public function addTag(Tag $tag) - { - $tag->setTask($this); - - $this->tags->add($tag); - } - - For a many-to-many relationship: - - // src/AppBundle/Entity/Task.php - - // ... public function addTag(Tag $tag) { + // for a many-to-many association: $tag->addTask($this); + // for a many-to-one association: + $tag->setTask($this); + $this->tags->add($tag); } - Inside ``Tag``, just make sure you have an ``addTask()`` method:: + If you're going for ``addTask()``, just make sure you have an appropriate method + that looks something like this:: // src/AppBundle/Entity/Tag.php