user4521750
user4521750

Reputation:

Add values before Doctrine Flush

I would like to add values before flushing. My others values are from a Form which is mapped with my Entity, so when i try to use setData(), my fields stay at "null".

Code :

    public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('BlogBlogBundle:Blog')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Blog entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createEditForm($entity);
    $editForm->handleRequest($request);

    if ($editForm->isValid()) {
        $editForm->get('date')->setData(date("Y-m-d"));
        $usr= $this->get('security.context')->getToken()->getUser();
        $usr->getUsername();
        $editForm->get('author')->setData($usr);
        var_dump($editForm);
        $em->flush();

        return $this->redirect($this->generateUrl('blogAdmin_edit', array('id' => $id)));
    }

    return $this->render('BlogBlogBundle:Admin:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

Upvotes: 0

Views: 317

Answers (2)

Anna Adamchuk
Anna Adamchuk

Reputation: 720

You have to change entity that you mapped to the form and use correct entity fields setters(I've tried to guess). Try to modify:

 ...
 if ($editForm->isSubmitted() && $editForm->isValid()) {
        $entity->setDate(date("Y-m-d"));
        $usr= $this->get('security.context')->getToken()->getUser();
        $usr->getUsername();
        $entity->setAuthor($usr);
        $em->persist($entity);
        $em->flush();
        ...
    }

Upvotes: 0

david00f
david00f

Reputation: 51

You dont need to use getData or setData on the form, you should use the setter of the entity itself. What you will save/persist is not the form, its the entity. The form is also using the setters on the entity class for the data which was sent.

like:

public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('BlogBlogBundle:Blog')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Blog entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createEditForm($entity);
    $editForm->handleRequest($request);

    if ($editForm->isValid()) {
        $usr = $this->get('security.context')->getToken()->getUser();
        //don't know what you wanna do with the username
        $usr->getUsername();
        //use the setter for the date... maybe $entity->setCreatedAt()
        $entity->setDate(date("Y-m-d"));
        //also use the setter for the user on the entity
        $entity->setAuthor($usr);
        var_dump($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('blogAdmin_edit', array('id' => $id)));
    }

    return $this->render('BlogBlogBundle:Admin:edit.html.twig', array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

Upvotes: 1

Related Questions