Skip to content

Instantly share code, notes, and snippets.

@coreymcmahon
Created May 5, 2012 04:02
Show Gist options
  • Select an option

  • Save coreymcmahon/2599562 to your computer and use it in GitHub Desktop.

Select an option

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
<?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