diff --git a/form/form_collections.rst b/form/form_collections.rst index fa650cbfa82..3441db6e799 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -12,27 +12,27 @@ that Task, right inside the same form. .. note:: In this article, it's loosely assumed that you're using Doctrine as your - database store. But if you're not using Doctrine (e.g. Propel or just - a database connection), it's all very similar. There are only a few parts - of this tutorial that really care about "persistence". + database store. But if you're not using Doctrine, it's all very similar. + There are only a few parts of this tutorial that really care about + persistence. If you *are* using Doctrine, you'll need to add the Doctrine metadata, - including the ``ManyToMany`` association mapping definition on the Task's - ``tags`` property. + including the ``ManyToMany`` association mapping definition on the + ``tags`` property of ``Task``. First, suppose that each ``Task`` belongs to multiple ``Tag`` objects. Start by creating a simple ``Task`` class:: - // src/AppBundle/Entity/Task.php - namespace AppBundle\Entity; + // src/App/Entity/Task.php + namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; class Task { - protected $description; + private $description; - protected $tags; + private $tags; public function __construct() { @@ -64,8 +64,8 @@ by creating a simple ``Task`` class:: Now, create a ``Tag`` class. As you saw above, a ``Task`` can have many ``Tag`` objects:: - // src/AppBundle/Entity/Tag.php - namespace AppBundle\Entity; + // src/App/Entity/Tag.php + namespace App\Entity; class Tag { @@ -84,10 +84,10 @@ objects:: Then, create a form class so that a ``Tag`` object can be modified by the user:: - // src/AppBundle/Form/Type/TagType.php - namespace AppBundle\Form\Type; + // src/App/Form/TagType.php + namespace App\Form; - use AppBundle\Entity\Tag; + use App\Entity\Tag; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -114,14 +114,15 @@ form itself, create a form for the ``Task`` class. Notice that you embed a collection of ``TagType`` forms using the :doc:`CollectionType ` field:: - // src/AppBundle/Form/Type/TaskType.php - namespace AppBundle\Form\Type; + // src/App/Form/TaskType.php + namespace App\Form; - use AppBundle\Entity\Task; + use App\Entity\Task; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Form\Extension\Core\Type\CollectionType; + use Symfony\Component\Form\Extension\Core\Type\SubmitType; class TaskType extends AbstractType { @@ -133,6 +134,7 @@ Notice that you embed a collection of ``TagType`` forms using the 'entry_type' => TagType::class, 'entry_options' => array('label' => false), )); + $builder->add('submit', SubmitType::class); } public function configureOptions(OptionsResolver $resolver) @@ -145,18 +147,23 @@ Notice that you embed a collection of ``TagType`` forms using the In your controller, you'll create a new form from the ``TaskType``:: - // src/AppBundle/Controller/TaskController.php - namespace AppBundle\Controller; + // src/App/Controller/TaskController.php + namespace App\Controller; - use AppBundle\Entity\Task; - use AppBundle\Entity\Tag; - use AppBundle\Form\Type\TaskType; use Symfony\Component\HttpFoundation\Request; - use Symfony\Bundle\FrameworkBundle\Controller\Controller; - - class TaskController extends Controller + use Symfony\Component\Routing\Annotation\Route; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; + use Doctrine\ORM\EntityManagerInterface; + use App\Entity\Task; + use App\Entity\Tag; + use App\Form\TaskType; + + class TaskController extends AbstractController { - public function newAction(Request $request) + /** + * @Route("/task/new") + */ + public function new(Request $request, EntityManagerInterface $em) { $task = new Task(); @@ -175,7 +182,9 @@ In your controller, you'll create a new form from the ``TaskType``:: $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - // ... maybe do some form processing, like saving the Task and Tag objects + // ... maybe do some form processing, like saving the Task and Tag objects: + $em->persist($task); + $em->flush(); } return $this->render('task/new.html.twig', array( @@ -186,16 +195,17 @@ In your controller, you'll create a new form from the ``TaskType``:: The corresponding template is now able to render both the ``description`` field for the task form as well as all the ``TagType`` forms for any tags -that are already related to this ``Task``. In the above controller, I added -some dummy code so that you can see this in action (since a ``Task`` has -zero tags when first created). +that are already related to this ``Task``. The above controller has some +dummy code so that you can see this in action (since a newly created ``Task`` +has zero tags). .. code-block:: html+twig - {# app/Resources/views/task/new.html.twig #} + {# templates/task/new.html.twig #} {# ... #} - + +