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');
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jul 21, 2016 at 13:01
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Review. Rather than the title being "Is this the best way ..." you might want to use "Post Editing on my Blog using Symfony2 and Doctrine. Ask the "Is this the best way" question within the body. This might draw more viewers and get you better answers. See codereview.stackexchange.com/help/how-to-ask \$\endgroup\$ Commented Jul 21, 2016 at 13:21
  • \$\begingroup\$ this link symfony.com/doc/current/doctrine.html#updating-an-object could be useful for you. \$\endgroup\$ Commented Aug 25, 2016 at 15:00

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!

answered Dec 20, 2016 at 22:53
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.