|
1 | 1 | .. index:: |
2 | 2 | single: Form; Embed collection of forms |
3 | 3 |
|
4 | | -How to Embed a Collection of Forms |
5 | | -================================== |
| 4 | +Edit Related Entities in a Single Form |
| 5 | +====================================== |
6 | 6 |
|
7 | | -In this article, you'll learn how to create a form that embeds a collection |
8 | | -of many other forms. This could be useful, for example, if you had a ``Task`` |
9 | | -class and you wanted to edit/create/remove many ``Tag`` objects related to |
10 | | -that Task, right inside the same form. |
| 7 | +To edit associated entities in the same form, Symfony has the concept of |
| 8 | +Form "Collections". Think of it as a series of sub-forms, embedded into |
| 9 | +the main form. |
11 | 10 |
|
12 | | -Let's start by creating a ``Task`` entity:: |
| 11 | +The example used in this article is a ``Task`` entity that relates to |
| 12 | +a ``Tag`` entitiy. The goal is to create a single form for tasks, that |
| 13 | +also allows to edit/create/remove many tags associated with that task. |
| 14 | + |
| 15 | +Here's the ``Task`` and the ``Tag`` entity:: |
13 | 16 |
|
14 | 17 | // src/Entity/Task.php |
15 | 18 | namespace App\Entity; |
@@ -45,12 +48,10 @@ Let's start by creating a ``Task`` entity:: |
45 | 48 |
|
46 | 49 | .. note:: |
47 | 50 |
|
48 | | - The ``ArrayCollection`` is specific to Doctrine and is basically the |
49 | | - same as using an ``array`` (but it must be an ``ArrayCollection`` if |
50 | | - you're using Doctrine). |
| 51 | + The `ArrayCollection`_ is specific to Doctrine and is basically the |
| 52 | + same as using an ``array``. |
51 | 53 |
|
52 | | -Now, create a ``Tag`` class. As you saw above, a ``Task`` can have many ``Tag`` |
53 | | -objects:: |
| 54 | +.. code-block:: php |
54 | 55 |
|
55 | 56 | // src/Entity/Tag.php |
56 | 57 | namespace App\Entity; |
@@ -688,3 +689,4 @@ the relationship between the removed ``Tag`` and ``Task`` object. |
688 | 689 | .. _`JSFiddle`: http://jsfiddle.net/847Kf/4/ |
689 | 690 | .. _`@a2lix/symfony-collection`: https://github.com/a2lix/symfony-collection |
690 | 691 | .. _`symfony-collection`: https://github.com/ninsuo/symfony-collection |
| 692 | +.. _`ArrayCollection`: https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html |
0 commit comments