Skip to content

Commit a09fb55

Browse files
committed
[book][forms] Adding details more on how data gets to the objects and how to persist in Doctrine
This was per comments from @dot-i-fy
1 parent 4888f6e commit a09fb55

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

book/forms.rst

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,23 @@ Now, when submitting the form, the controller binds the submitted data to the
219219
form, which translates that data back to the ``name`` and ``price`` properties
220220
of the ``$product`` object. This all happens via the ``bindRequest()`` method.
221221

222+
.. note::
223+
224+
As soon as ``bindRequest()`` is called, the submitted data is transferred
225+
to the underlying object immediately. For example, imagine that ``Foo``
226+
is submitted for the ``name`` field:
227+
228+
.. code-block:: php
229+
230+
$product = new Product();
231+
$product->name = 'Test product';
232+
233+
$product->bindRequest($this->get('request'));
234+
echo $product->name;
235+
236+
The above statement will echo ``Foo``, because ``bindRequest`` ultimately
237+
moves the submitted data back to the ``$product`` object.
238+
222239
This controller follows a common pattern for handling forms, and has three
223240
possible paths:
224241

@@ -648,14 +665,20 @@ when the form is valid:
648665
.. code-block:: php
649666
650667
if ($form->isValid()) {
651-
// persist the $product object to the database
652-
// or do anything else you need to do
668+
$em = $this->get('doctrine')->getEntityManager();
669+
$em->persist($product);
670+
$em->flush();
653671
654672
return $this->redirect($this->generateUrl('store_product_success'));
655673
}
656674
657675
For more information, see the :doc:`Doctrine ORM chapter</book/doctrine/orm>`.
658676

677+
The key thing to understand is that when the form is bound, the submitted
678+
data is transferred to the underlying object immediately. If you want to
679+
persist that data, you simply need to persist the object itself (which already
680+
contains the submitted data).
681+
659682
If the underlying object of a form (e.g. ``Product``) happens to be mapped
660683
with the Doctrine ORM, the form framework will use that information - along
661684
with the validation metadata - to guess the type of a particular field.

0 commit comments

Comments
 (0)