Advertisement
Guest User

simple crud aja

a guest
Sep 9th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. namespace Acme\TestBundle\Controller;
  3.  
  4. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Acme\TestBundle\Entity\Shouter;
  9.  
  10. /**
  11.  * @Route("/acme")
  12.  */
  13. class HelloDbController extends Controller {
  14.    
  15.     /**
  16.      * @Route("/insert_db/{msg}", name="hai_insert_db")
  17.      */
  18.     public function insertdbAction($msg = 'hallo world ')
  19.     {
  20.    
  21.         $shouter = new Shouter();
  22.  
  23.         $shouter->setMessages($msg);
  24.         $shouter->setDescription("lorema ipsuma");
  25.         $shouter->setCreatedAt(new \DateTime("now"));
  26.         $shouter->setUpdatedAt(new \DateTime("now"));
  27.  
  28.         $em = $this->getDoctrine()->getManager();
  29.         $em->persist($shouter);
  30.         $em->flush();
  31.        
  32.         return new Response('Created shouter id '.$shouter->getId());
  33.     }
  34.    
  35.     /**
  36.      * @Route("/hai_db", name="hai_db")
  37.      */
  38.     public function greatingAction()
  39.     {
  40.         $repository = $this->getDoctrine()->getRepository('AcmeTestBundle:Shouter');
  41.         $shouters   = $repository->findAll();
  42.        
  43.         $html = $this->container
  44.         ->get('templating')
  45.         ->render(
  46.                 'AcmeTestBundle:Hoi:list.html.twig',
  47.                 array('shouters' => $shouters)
  48.         );
  49.  
  50.         return new Response($html);
  51.     }
  52.    
  53.     /**
  54.      *
  55.      * @Route("/hai_form_db", name="hai_form_add_db1")
  56.      */
  57.     public function formAdd1Action(Request $request) {
  58.         $shouter = new Shouter();
  59.        
  60.         $form = $this->createFormBuilder($shouter)
  61.         ->add('messages', 'text')
  62.         ->add('description', 'text')
  63.         ->add('createdAt', 'datetime')
  64.         ->add('updatedat', 'datetime')
  65.         ->add('save', 'submit', array('label' => 'Create Hello World'))
  66.         ->getForm();
  67.        
  68.         //handle save
  69.         $form->handleRequest($request);
  70.         if ($form->isValid()) {
  71.             $data = $form->getData();
  72.            
  73.             $em = $this->getDoctrine()->getManager();
  74.             $em->persist($data);
  75.             $em->flush();
  76.             //return $this->redirect('task_success');
  77.         }
  78.        
  79.         return $this->render('AcmeTestBundle:Hoi:add.html.twig', array(
  80.                 'form' => $form->createView(),
  81.         ));
  82.     }
  83.    
  84.    
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement