\$\begingroup\$
\$\endgroup\$
2
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
-
\$\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\$pacmaninbw– pacmaninbw ♦2016年07月21日 13:21:17 +00:00Commented Jul 21, 2016 at 13:21
-
\$\begingroup\$ this link symfony.com/doc/current/doctrine.html#updating-an-object could be useful for you. \$\endgroup\$yceruto– yceruto2016年08月25日 15:00:42 +00:00Commented Aug 25, 2016 at 15:00
1 Answer 1
\$\begingroup\$
\$\endgroup\$
You need to add persist()
like so:
...
$post->setAuthor($formData->getAuthor());
$em->persist($post);
$em->flush();
...
That should do it!
lang-php