Skip to content

Explaining the "one-to-many" case more explicitly #9259

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
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misleading, in fact it's the opposite here (i.e. many-to-one). Maybe we should just manage this using inline comments like this:

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);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// 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

Expand All @@ -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
Expand Down