Skip to content

Instantly share code, notes, and snippets.

@weaverryan weaverryan/MoveController.php Secret
Created Oct 4, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.