Advertisement
adriandrozdz

OwnerController

Jul 28th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.42 KB | None | 0 0
  1. <?php
  2. namespace Inventory\Controller;
  3.  
  4. use Zend\Mvc\Controller\AbstractActionController;
  5. use Zend\View\Model\ViewModel;
  6. use Doctrine\ORM\EntityManager;
  7. use Inventory\Entity\Owners;
  8. use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
  9. use DoctrineORMModule\Form\Annotation\AnnotationBuilder;
  10. use Doctrine\ORM\Query;
  11.  
  12. class OwnerController extends AbstractActionController
  13. {
  14.         /**            
  15.     * @var Doctrine\ORM\EntityManager
  16.     */      
  17.     protected $em;
  18.    
  19.     public function indexAction()
  20.     {
  21.         $dql = "SELECT o FROM \Inventory\Entity\Owners o";
  22.         $query = $this->getEntityManager()->createQuery($dql);
  23.         $data = $query->getArrayResult(\Doctrine\ORM\Query::HYDRATE_SIMPLEOBJECT);
  24.         return array('owners' => $data);
  25.     }
  26.    
  27.     public function addAction()
  28.     {
  29.         $entityManager  = $this->getEntityManager();
  30.         $owner = new Owners();
  31.         $builder = new AnnotationBuilder($entityManager);
  32.         $form = $builder->createForm($owner);
  33.         $form->setHydrator(new DoctrineHydrator($entityManager, 'Inventory\Entity\Owners'));
  34.         $form->add(array(
  35.             'name' => 'submit',
  36.             'type' => 'Submit',
  37.             'attributes' => array(
  38.                 'id' => 'submit',
  39.                 'class' => 'btn btn-primary',
  40.                 'value' => 'Dodaj'
  41.         )));
  42.         $form->bind($owner);
  43.        
  44.         if ($this->request->isPost()) {
  45.             $form->setData($this->request->getPost());
  46.            
  47.             if ($form->isValid()) {
  48.                 $entityManager->persist($owner);
  49.                 $entityManager->flush();
  50.                 $this->redirect()->toRoute('owners');
  51.             }
  52.         }
  53.         return array('form' => $form);
  54.     }
  55.    
  56.     public function editAction()
  57.     {
  58.         $id = (int) $this->getEvent()->getRouteMatch()->getParam('id');
  59.         if (!$id) {
  60.             return $this->redirect()->toRoute('owners', array('action' => 'add'));
  61.         }
  62.        
  63.         $entityManager  = $this->getEntityManager();
  64.         $owner = $this->getEntityManager()->find('Inventory\Entity\Owners', $id);
  65.  
  66.         $builder = new AnnotationBuilder($entityManager);
  67.         $form = $builder->createForm($owner);
  68.         $form->setHydrator(new DoctrineHydrator($entityManager, 'Inventory\Entity\Owners'));
  69.         $form->bind($owner);
  70.         $form->add(array(
  71.             'name' => 'submit',
  72.             'type' => 'Submit',
  73.             'attributes' => array(
  74.                 'id' => 'submit',
  75.                 'class' => 'btn btn-primary',
  76.                 'value' => 'Zapisz zmiany'
  77.             )
  78.         ));
  79.        
  80.         $request = $this->getRequest();
  81.         if ($request->isPost()) {
  82.             $form->setData($request->getPost());
  83.             if ($form->isValid()) {
  84.                     //$form->bindValues();
  85.                     $entityManager->persist($owner);
  86.                     $entityManager->flush();
  87.                    
  88.                     $this->redirect()->toRoute('owners');
  89.             }
  90.         }
  91.         return array(
  92.             'id' => $id,
  93.             'form' => $form
  94.         );
  95.     }
  96.  
  97.     public function deleteAction()
  98.     {
  99.         $id = (int) $this->getEvent()->getRouteMatch()->getParam('id');
  100.         if (!$id) {
  101.             return $this->redirect()->toRoute('owners', array('action' => 'add'));
  102.         }
  103.        
  104.         $ownerName = $this->getEntityManager()->find('Inventory\Entity\Owners', $id)->getName();
  105.         if (!$ownerName) {
  106.             throw new Exception('Nie ma takiego właściciela');
  107.         }
  108.        
  109.         $q = $this->getEntityManager()
  110.                               ->createQueryBuilder();
  111.         $q->select('Owner', 'List', 'Books')
  112.             ->from('\Inventory\Entity\Owners', 'Owner')
  113.             ->join('Owner.bookList', 'List')
  114.             ->join('List.book', 'Books')
  115.             ->where('Owner.id = :id')
  116.             ->setParameter('id', $id);
  117.         $query = $q->getQuery();
  118.         $owner = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
  119.        
  120.        
  121.        
  122.        
  123.         return array(
  124.             'owner' => $owner,
  125.             'name' => $ownerName,
  126.         );
  127.     }
  128.    
  129.     public function getEntityManager()
  130.     {
  131.         if (null === $this->em) {
  132.             $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
  133.         }
  134.         return $this->em;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement