Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Created October 26, 2012 16:29
Show Gist options
  • Save jmolivas/3959754 to your computer and use it in GitHub Desktop.
Save jmolivas/3959754 to your computer and use it in GitHub Desktop.
Controller - $app/console generate:doctrine:crud on #Symfony 2.1
/**
* Edits an existing Project entity.
*
* @Route("/{id}", name="project_update")
* @Method("PUT")
* @Template("ProjectBundle:Project:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ProjectBundle:Project')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Project entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new ProjectType(), $entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
$this->get('session')->setFlash('success', 'Project updated ...');
return $this->redirect($this->generateUrl('project_edit', array('id' => $id)));
}
else {
$this->get('session')->setFlash('error', 'Invalid data...');
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Project entity.
*
* @Route("/{id}", name="project_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ProjectBundle:Project')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Project entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('project'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment