Skip to content

Instantly share code, notes, and snippets.

@weaverryan
Created October 4, 2017 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weaverryan/0509e15af924a9f3d970dd9e18dc6dde to your computer and use it in GitHub Desktop.
Save weaverryan/0509e15af924a9f3d970dd9e18dc6dde to your computer and use it in GitHub Desktop.
KnpUniversity: Taking a form's model data and putting it on an entity
<?php
public function addAction(Request $request)
{
$form = $this->createForm(MoviePersonForm::class);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
// Because your MoviePersonForm has a data_class set to MoviePersonModel,
// when you call $form->getData(), it gives you a MoviePersonModel object
// that's nice, because you can use it's getters/setters to easily access data
$moviePersonModel = $form->getData();
// down here, your job is to populate the Movie object by using the $moviePersonModel
// The $movie variable is an object, so you need to call methods on it
// e.g. $movie->setName(..) instead of $movie['name'] (I'm assuming your Movie class
// as a setName() method on it, but I don't know for sure, because I haven't seen that class!)
$movie = new Movie();
$movie->setName($moviePersonModel->getMovieName());
$movie->setYear($moviePersonModel->getMovieYear());
$movie->setDescription($moviePersonModel->getMovieDescription());
$em = $this->getDoctrine()->getManager();
$em->persist($movie);
$em->flush();
$this->addFlash('success', 'Movie created');
return $this->redirectToRoute('cms_list_movie');
}
return $this->render('cms/movie/crew.html.twig',[
'MoviePersonForm' => $form->createView()
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment