mario
mario

Reputation: 624

Symfony2 Embedded forms, How to persist objects

I am trying to implement Embedded Forms (Symfony2, 2.7), with Task and Tag entities, One2Many.

To save reference to the Task object into a Tag record, I am able to define Task's createAction() only by:

/**
 * Creates a new Task entity.
 *
 * @Route("/", name="MyName_Task_create")
 * @Method("POST")
 * @Template("MyNameBundleBlogBundle:Task:new.html.twig")
 */
public function createAction(Request $request)
{
    $task = new Task();
    $form = $this->createCreateForm($task);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $tags = $task->getTags();
        foreach($tags as $tg){$tg->setTask($task); $em->persist($tg);}   // <-- do I really need to loop?
        $em->persist($task);
        $em->flush();

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

    return array(
        'entity' => $task,
        'form'   => $form->createView(),
    );
}

EDIT: I know it should work without the loop straightforwardly, but it does not. Question is: What should I look for which I might have written wrong? See related question

Note, I have:

class Task{
....
 /**
 *
 * @ORM\OneToMany(targetEntity="Tag", mappedBy="Task", cascade={"persist"} )
 */
private $Tags;
....
 /**
 * Add tag
 *
 * @param \MyName\Bundle\BlogBundle\Entity\Tag $tag
 *
 * @return Task
 */
public function addTag(\MyName\Bundle\BlogBundle\Entity\Tag $tag)
{
    $this->tags[] = $tag;
    $tag->setTask($this);
    return $this;
}
}

Upvotes: 3

Views: 874

Answers (2)

Turdaliev Nursultan
Turdaliev Nursultan

Reputation: 2576

No, you don't need to loop through all tags and explicitly set task, Symfony will do that for you if you configure it correctly.

The only thing you need to add is set by_reference to false inside your form builder. In this case, symfony will explicitly will call setTask on every tag.

For more info 'by_reference'

Upvotes: 1

chalasr
chalasr

Reputation: 13167

According to @Cerad comment, the only thing you have to do is persist the Task.

All related tags will be automatically persisted, thanks to cascade={"persist"} in your association mapping.

Your code should be :

if ($form->isValid()) {
    $em = $this->getDoctrine()->getManager();
    $em->persist($task);
    $em->flush();

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

See the Etablishing associations and Cascade operations chapters of Doctrine documentation.

Upvotes: 0

Related Questions