Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Inventory\Controller;
- use Zend\Mvc\Controller\AbstractActionController;
- use Zend\View\Model\ViewModel;
- use Doctrine\ORM\EntityManager;
- use Inventory\Entity\Owners;
- use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
- use DoctrineORMModule\Form\Annotation\AnnotationBuilder;
- use Doctrine\ORM\Query;
- class OwnerController extends AbstractActionController
- {
- /**
- * @var Doctrine\ORM\EntityManager
- */
- protected $em;
- public function indexAction()
- {
- $dql = "SELECT o FROM \Inventory\Entity\Owners o";
- $query = $this->getEntityManager()->createQuery($dql);
- $data = $query->getArrayResult(\Doctrine\ORM\Query::HYDRATE_SIMPLEOBJECT);
- return array('owners' => $data);
- }
- public function addAction()
- {
- $entityManager = $this->getEntityManager();
- $owner = new Owners();
- $builder = new AnnotationBuilder($entityManager);
- $form = $builder->createForm($owner);
- $form->setHydrator(new DoctrineHydrator($entityManager, 'Inventory\Entity\Owners'));
- $form->add(array(
- 'name' => 'submit',
- 'type' => 'Submit',
- 'attributes' => array(
- 'id' => 'submit',
- 'class' => 'btn btn-primary',
- 'value' => 'Dodaj'
- )));
- $form->bind($owner);
- if ($this->request->isPost()) {
- $form->setData($this->request->getPost());
- if ($form->isValid()) {
- $entityManager->persist($owner);
- $entityManager->flush();
- $this->redirect()->toRoute('owners');
- }
- }
- return array('form' => $form);
- }
- public function editAction()
- {
- $id = (int) $this->getEvent()->getRouteMatch()->getParam('id');
- if (!$id) {
- return $this->redirect()->toRoute('owners', array('action' => 'add'));
- }
- $entityManager = $this->getEntityManager();
- $owner = $this->getEntityManager()->find('Inventory\Entity\Owners', $id);
- $builder = new AnnotationBuilder($entityManager);
- $form = $builder->createForm($owner);
- $form->setHydrator(new DoctrineHydrator($entityManager, 'Inventory\Entity\Owners'));
- $form->bind($owner);
- $form->add(array(
- 'name' => 'submit',
- 'type' => 'Submit',
- 'attributes' => array(
- 'id' => 'submit',
- 'class' => 'btn btn-primary',
- 'value' => 'Zapisz zmiany'
- )
- ));
- $request = $this->getRequest();
- if ($request->isPost()) {
- $form->setData($request->getPost());
- if ($form->isValid()) {
- //$form->bindValues();
- $entityManager->persist($owner);
- $entityManager->flush();
- $this->redirect()->toRoute('owners');
- }
- }
- return array(
- 'id' => $id,
- 'form' => $form
- );
- }
- public function deleteAction()
- {
- $id = (int) $this->getEvent()->getRouteMatch()->getParam('id');
- if (!$id) {
- return $this->redirect()->toRoute('owners', array('action' => 'add'));
- }
- $ownerName = $this->getEntityManager()->find('Inventory\Entity\Owners', $id)->getName();
- if (!$ownerName) {
- throw new Exception('Nie ma takiego właściciela');
- }
- $q = $this->getEntityManager()
- ->createQueryBuilder();
- $q->select('Owner', 'List', 'Books')
- ->from('\Inventory\Entity\Owners', 'Owner')
- ->join('Owner.bookList', 'List')
- ->join('List.book', 'Books')
- ->where('Owner.id = :id')
- ->setParameter('id', $id);
- $query = $q->getQuery();
- $owner = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
- return array(
- 'owner' => $owner,
- 'name' => $ownerName,
- );
- }
- public function getEntityManager()
- {
- if (null === $this->em) {
- $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
- }
- return $this->em;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement