Created
May 5, 2012 04:02
-
-
Save coreymcmahon/2599562 to your computer and use it in GitHub Desktop.
Example of a controller using a Form to persist data to the DB - http://www.symfonycentral.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* ... rest of the controller */ | |
| if ($this->getRequest()->getMethod() == 'POST') { | |
| /* First we 'bind' the postback data to the form */ | |
| $form->bind($this->getRequest()->get('form')); | |
| if ($form->isValid()) { | |
| /* Now we get the data as an associative array */ | |
| $formdata = $form->getData(); | |
| /* Create the entity and add each of the fields */ | |
| $article = new Post(); | |
| $article->setTitle($formdata['title']) | |
| ->setBody($formdata['body']); | |
| /* Persist the object to the database */ | |
| $em = $this->getDoctrine()->getEntityManager(); | |
| $em->persist($article); | |
| $em->flush(); | |
| } | |
| } | |
| /* ... rest of the controller */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment