0
\$\begingroup\$

I want to make post editing on my blog. I get data from a form and want to put it into a database. I'm not sure if using setters is the best way, so maybe you can show me better solution?

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
  $formData = $form->getData();

  $post->setTitle($formData->getTitle());
  $post->setText($formData->getText());
  $post->setAuthor($formData->getAuthor());
  $em->flush();

  $this->addFlash(
    'notice',
    'Post updated!'
  );

  return $this->redirectToRoute('admin');
}
\$\endgroup\$
2

1 Answer 1

1
\$\begingroup\$

You need to add persist() like so:

...
$post->setAuthor($formData->getAuthor());
$em->persist($post);
$em->flush();
...

That should do it!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.