Permalink
Browse files

add doctrine crud generator with support bootstrap

  • Loading branch information...
Pentium133 committed Nov 18, 2011
1 parent 849eb14 commit a5318778d6e4c38f366f1fc1f369b92ec7e6e481
@@ -0,0 +1,22 @@
+<?php
+
+namespace Mopa\BootstrapBundle\Command;
+
+use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator;
+use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand as BaseGenerator;
+
+class DoctrineCrudCommand extends BaseGenerator
+{
+ protected function configure()
+ {
+ parent::configure();
+ $this->setName('mopa:generate:crud');
+ }
+
+ protected function getGenerator()
+ {
+ $generator = new DoctrineCrudGenerator($this->getContainer()->get('filesystem'), __DIR__.'/../Resources/skeleton/crud');
+ $this->setGenerator($generator);
+ return parent::getGenerator();
+ }
+}
@@ -0,0 +1,42 @@
+
+ /**
+ * Creates a new {{ entity }} entity.
+ *
+{% if 'annotation' == format %}
+ * @Route("/create", name="{{ route_name_prefix }}_create")
+ * @Method("post")
+ * @Template("{{ bundle }}:{{ entity }}:new.html.twig")
+{% endif %}
+ */
+ public function createAction()
+ {
+ $entity = new {{ entity_class }}();
+ $request = $this->getRequest();
+ $form = $this->createForm(new {{ entity_class }}Type(), $entity);
+ $form->bindRequest($request);
+
+ if ($form->isValid()) {
+ $em = $this->getDoctrine()->getEntityManager();
+ $em->persist($entity);
+ $em->flush();
+
+ {% if 'show' in actions -%}
+ return $this->redirect($this->generateUrl('{{ route_name_prefix }}_show', array('id' => $entity->getId())));
+ {% else -%}
+ return $this->redirect($this->generateUrl('{{ route_name_prefix }}'));
+ {%- endif %}
+
+ }
+
+{% if 'annotation' == format %}
+ return array(
+ 'entity' => $entity,
+ 'form' => $form->createView()
+ );
+{% else %}
+ return $this->render('{{ bundle }}:{{ entity|replace({'\\': '/'}) }}:new.html.twig', array(
+ 'entity' => $entity,
+ 'form' => $form->createView()
+ ));
+{% endif %}
+ }
@@ -0,0 +1,38 @@
+
+ /**
+ * Deletes a {{ entity }} entity.
+ *
+{% if 'annotation' == format %}
+ * @Route("/{id}/delete", name="{{ route_name_prefix }}_delete")
+ * @Method("post")
+{% endif %}
+ */
+ public function deleteAction($id)
+ {
+ $form = $this->createDeleteForm($id);
+ $request = $this->getRequest();
+
+ $form->bindRequest($request);
+
+ if ($form->isValid()) {
+ $em = $this->getDoctrine()->getEntityManager();
+ $entity = $em->getRepository('{{ bundle }}:{{ entity }}')->find($id);
+
+ if (!$entity) {
+ throw $this->createNotFoundException('Unable to find {{ entity }} entity.');
+ }
+
+ $em->remove($entity);
+ $em->flush();
+ }
+
+ return $this->redirect($this->generateUrl('{{ route_name_prefix }}'));
+ }
+
+ private function createDeleteForm($id)
+ {
+ return $this->createFormBuilder(array('id' => $id))
+ ->add('id', 'hidden')
+ ->getForm()
+ ;
+ }
@@ -0,0 +1,36 @@
+
+ /**
+ * Displays a form to edit an existing {{ entity }} entity.
+ *
+{% if 'annotation' == format %}
+ * @Route("/{id}/edit", name="{{ route_name_prefix }}_edit")
+ * @Template()
+{% endif %}
+ */
+ public function editAction($id)
+ {
+ $em = $this->getDoctrine()->getEntityManager();
+
+ $entity = $em->getRepository('{{ bundle }}:{{ entity }}')->find($id);
+
+ if (!$entity) {
+ throw $this->createNotFoundException('Unable to find {{ entity }} entity.');
+ }
+
+ $editForm = $this->createForm(new {{ entity_class }}Type(), $entity);
+ $deleteForm = $this->createDeleteForm($id);
+
+{% if 'annotation' == format %}
+ return array(
+ 'entity' => $entity,
+ 'edit_form' => $editForm->createView(),
+ 'delete_form' => $deleteForm->createView(),
+ );
+{% else %}
+ return $this->render('{{ bundle }}:{{ entity|replace({'\\': '/'}) }}:edit.html.twig', array(
+ 'entity' => $entity,
+ 'edit_form' => $editForm->createView(),
+ 'delete_form' => $deleteForm->createView(),
+ ));
+{% endif %}
+ }
@@ -0,0 +1,23 @@
+
+ /**
+ * Lists all {{ entity }} entities.
+ *
+{% if 'annotation' == format %}
+ * @Route("/", name="{{ route_name_prefix }}")
+ * @Template()
+{% endif %}
+ */
+ public function indexAction()
+ {
+ $em = $this->getDoctrine()->getEntityManager();
+
+ $entities = $em->getRepository('{{ bundle }}:{{ entity }}')->findAll();
+
+{% if 'annotation' == format %}
+ return array('entities' => $entities);
+{% else %}
+ return $this->render('{{ bundle }}:{{ entity|replace({'\\': '/'}) }}:index.html.twig', array(
+ 'entities' => $entities
+ ));
+{% endif %}
+ }
@@ -0,0 +1,26 @@
+
+ /**
+ * Displays a form to create a new {{ entity }} entity.
+ *
+{% if 'annotation' == format %}
+ * @Route("/new", name="{{ route_name_prefix }}_new")
+ * @Template()
+{% endif %}
+ */
+ public function newAction()
+ {
+ $entity = new {{ entity_class }}();
+ $form = $this->createForm(new {{ entity_class }}Type(), $entity);
+
+{% if 'annotation' == format %}
+ return array(
+ 'entity' => $entity,
+ 'form' => $form->createView()
+ );
+{% else %}
+ return $this->render('{{ bundle }}:{{ entity|replace({'\\': '/'}) }}:new.html.twig', array(
+ 'entity' => $entity,
+ 'form' => $form->createView()
+ ));
+{% endif %}
+ }
@@ -0,0 +1,41 @@
+
+ /**
+ * Finds and displays a {{ entity }} entity.
+ *
+{% if 'annotation' == format %}
+ * @Route("/{id}/show", name="{{ route_name_prefix }}_show")
+ * @Template()
+{% endif %}
+ */
+ public function showAction($id)
+ {
+ $em = $this->getDoctrine()->getEntityManager();
+
+ $entity = $em->getRepository('{{ bundle }}:{{ entity }}')->find($id);
+
+ if (!$entity) {
+ throw $this->createNotFoundException('Unable to find {{ entity }} entity.');
+ }
+{% if 'delete' in actions %}
+
+ $deleteForm = $this->createDeleteForm($id);
+{% endif %}
+
+{% if 'annotation' == format %}
+ return array(
+ 'entity' => $entity,
+{% if 'delete' in actions %}
+ 'delete_form' => $deleteForm->createView(),
+
+{%- endif %}
+ );
+{% else %}
+ return $this->render('{{ bundle }}:{{ entity|replace({'\\': '/'}) }}:show.html.twig', array(
+ 'entity' => $entity,
+{% if 'delete' in actions %}
+ 'delete_form' => $deleteForm->createView(),
+
+{% endif %}
+ ));
+{% endif %}
+ }
@@ -0,0 +1,48 @@
+
+ /**
+ * Edits an existing {{ entity }} entity.
+ *
+{% if 'annotation' == format %}
+ * @Route("/{id}/update", name="{{ route_name_prefix }}_update")
+ * @Method("post")
+ * @Template("{{ bundle }}:{{ entity }}:edit.html.twig")
+{% endif %}
+ */
+ public function updateAction($id)
+ {
+ $em = $this->getDoctrine()->getEntityManager();
+
+ $entity = $em->getRepository('{{ bundle }}:{{ entity }}')->find($id);
+
+ if (!$entity) {
+ throw $this->createNotFoundException('Unable to find {{ entity }} entity.');
+ }
+
+ $editForm = $this->createForm(new {{ entity_class }}Type(), $entity);
+ $deleteForm = $this->createDeleteForm($id);
+
+ $request = $this->getRequest();
+
+ $editForm->bindRequest($request);
+
+ if ($editForm->isValid()) {
+ $em->persist($entity);
+ $em->flush();
+
+ return $this->redirect($this->generateUrl('{{ route_name_prefix }}_edit', array('id' => $id)));
+ }
+
+{% if 'annotation' == format %}
+ return array(
+ 'entity' => $entity,
+ 'edit_form' => $editForm->createView(),
+ 'delete_form' => $deleteForm->createView(),
+ );
+{% else %}
+ return $this->render('{{ bundle }}:{{ entity|replace({'\\': '/'}) }}:edit.html.twig', array(
+ 'entity' => $entity,
+ 'edit_form' => $editForm->createView(),
+ 'delete_form' => $deleteForm->createView(),
+ ));
+{% endif %}
+ }
@@ -0,0 +1,52 @@
+<?php
+
+use Symfony\Component\Routing\RouteCollection;
+use Symfony\Component\Routing\Route;
+
+$collection = new RouteCollection();
+
+{% if 'index' in actions %}
+$collection->add('{{ route_name_prefix }}', new Route('/', array(
+ '_controller' => '{{ bundle }}:{{ entity }}:index',
+)));
+{% endif %}
+
+{% if 'show' in actions %}
+$collection->add('{{ route_name_prefix }}_show', new Route('/{id}/show', array(
+ '_controller' => '{{ bundle }}:{{ entity }}:show',
+)));
+{% endif %}
+
+{% if 'new' in actions %}
+$collection->add('{{ route_name_prefix }}_new', new Route('/new', array(
+ '_controller' => '{{ bundle }}:{{ entity }}:new',
+)));
+
+$collection->add('{{ route_name_prefix }}_create', new Route(
+ '/create',
+ array('_controller' => '{{ bundle }}:{{ entity }}:create'),
+ array('_method' => 'post')
+));
+{% endif %}
+
+{% if 'edit' in actions %}
+$collection->add('{{ route_name_prefix }}_edit', new Route('/{id}/edit', array(
+ '_controller' => '{{ bundle }}:{{ entity }}:edit',
+)));
+
+$collection->add('{{ route_name_prefix }}_update', new Route(
+ '/{id}/update',
+ array('_controller' => '{{ bundle }}:{{ entity }}:update'),
+ array('_method' => 'post')
+));
+{% endif %}
+
+{% if 'delete' in actions %}
+$collection->add('{{ route_name_prefix }}_delete', new Route(
+ '/{id}/delete',
+ array('_controller' => '{{ bundle }}:{{ entity }}:delete'),
+ array('_method' => 'post')
+));
+{% endif %}
+
+return $collection;
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<routes xmlns="http://symfony.com/schema/routing"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
+
+ <route id="{{ route_name_prefix }}" pattern="/">
+ <default key="_controller">{{ bundle }}:{{ entity }}:index</default>
+ </route>
+
+ <route id="{{ route_name_prefix }}_show" pattern="/{id}/show">
+ <default key="_controller">{{ bundle }}:{{ entity }}:show</default>
+ </route>
+
+{% if 'new' in actions %}
+ <route id="{{ route_name_prefix }}_new" pattern="/new">
+ <default key="_controller">{{ bundle }}:{{ entity }}:new</default>
+ </route>
+
+ <route id="{{ route_name_prefix }}_create" pattern="/create">
+ <default key="_controller">{{ bundle }}:{{ entity }}:create</default>
+ <requirement key="_method">post</requirement>
+ </route>
+{% endif %}
+
+{% if 'edit' in actions %}
+ <route id="{{ route_name_prefix }}_edit" pattern="/{id}/edit">
+ <default key="_controller">{{ bundle }}:{{ entity }}:edit</default>
+ </route>
+
+ <route id="{{ route_name_prefix }}_update" pattern="/{id}/update">
+ <default key="_controller">{{ bundle }}:{{ entity }}:update</default>
+ <requirement key="_method">post</requirement>
+ </route>
+{% endif %}
+
+{% if 'delete' in actions %}
+ <route id="{{ route_name_prefix }}_delete" pattern="/{id}/delete">
+ <default key="_controller">{{ bundle }}:{{ entity }}:delete</default>
+ <requirement key="_method">post</requirement>
+ </route>
+{% endif %}
+
+</routes>
Oops, something went wrong.

0 comments on commit a531877

Please sign in to comment.