E
E
Eugene Wolf2016-11-11 20:52:35
symfony
Eugene Wolf, 2016-11-11 20:52:35

Can you please explain how updating a record in a Symfony/Doctrine database works?

I am learning Symfony-3. Generated entities, created forms, generated CRUD... For example, we took the vendor table , which consists of 3 simple fields: id, title, email.
The standard code from the CRUD generator, which is not completely clear to me:

/**
     * Displays a form to edit an existing vendor entity.
     *
     * @Route("/{id}/edit", name="vendor_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Vendor $vendor)
    {
        $deleteForm = $this->createDeleteForm($vendor);
        $editForm = $this->createForm('AppBundle\Form\VendorType', $vendor);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('vendor_edit', array('id' => $vendor->getId()));
        }

        return $this->render('vendor/edit.html.twig', array(
            'vendor' => $vendor,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

I don't quite understand two things:
public function editAction(Request $request, Vendor $vendor)

Here, in addition to the request, we also pass the Vendor object . I don't quite understand this logic. If we passed two forms at once, for example Vendor and User , would the User object also appear and it would be automatically assigned to the $user variable? Usually, I pulled out the necessary data directly from the $request variable and assigned it "by hand" where I needed (although I certainly like this option too).
The second point is not clear to me:
if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

Here, in the first line, there is a check that the form is filled out, and then some magic happens that is not clear to me. We don't have any variables, but we do a buffer flush. How does Doctrine know what and where to write? I did not find anywhere in the controller code any link connecting the second line of code with any object.
Please explain how it all works, or if it's not difficult - poke into the documentation, where you could read about it. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Skobkin, 2016-11-11
@Wolfnsex

If we had two forms passed at once, for example, Vendor and User, would the User object also appear and would it be automatically assigned to the $user variable? Usually, I pulled out the necessary data directly from the $request variable and assigned it "by hand" where I needed (although I certainly like this option too).

No magic. Learn how an ORM like Data Mapper works. You have an Entity Manager that persists certain entities. In this case, your entity gets into it when processing the form transparently (as if behind the scenes) through the so-called DataTransformers .
In general, see what Flush is in relation to this ORM. For example, here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question