Guest User

Untitled

a guest
Oct 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this template, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7.  
  8. namespace Ticket\Account\Entity\Repository;
  9.  
  10. use Ticket;
  11. use Signifymedia;
  12. use Doctrine;
  13.  
  14. class AccoutableRepository extends Doctrine\ORM\EntityRepository implements IAccountableRepository
  15. {
  16.  
  17.     /** @var Ticket\Account\Entity\AccountEntity */
  18.     protected $actualAccount = null;
  19.  
  20.     public function setAccount(\Ticket\Account\Entity\AccountEntity $account)
  21.     {
  22.         $this->actualAccount = $account;
  23.     }
  24.  
  25.     public function createQueryBuilder($alias)
  26.     {
  27.         $q = parent::createQueryBuilder($alias);
  28.         if (is_null($this->actualAccount)) {
  29.             return $q;
  30.         }
  31.         return $q->add("where", "$alias.account = :account")
  32.                 ->setParameter('account', $this->actualAccount->getId());
  33.     }
  34.  
  35.     public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  36.     {
  37.         if (!is_null($this->actualAccount)) {
  38.             $criteria['account'] = $this->actualAccount->getId();
  39.         }
  40.         return parent::findBy($criteria, $orderBy, $limit, $offset);
  41.     }
  42.  
  43.     public function findOneBy(array $criteria)
  44.     {
  45.         $criteria = array();
  46.         if (!is_null($this->actualAccount)) {
  47.             $criteria['account'] = $this->actualAccount->getId();
  48.         }
  49.         return parent::findOneBy($criteria);
  50.     }
  51.  
  52.     public function find($id, $lockMode = Doctrine\DBAL\LockMode::NONE, $lockVersion = null)
  53.     {
  54.         $entity = parent::find($id, $lockMode, $lockVersion);
  55.         if (!is_null($entity)) {
  56.             $accountFiled = \Nette\Reflection\ClassType::from($entity)->getProperty('account');
  57.             $accountFiled->setAccessible(true);
  58.             if ($accountFiled->getValue($entity)->getId() !== $this->actualAccount->getId()) {
  59.                 $this->_em->detach($entity);
  60.                 $entity = null;
  61.             }
  62.         }
  63.         return $entity;
  64.     }
  65.  
  66. }
  67.  
  68.  
  69.  
  70. abstract class BasePresenter extends \Nette\Application\UI\Presenter
  71. {
  72.     protected function startup()
  73.     {
  74.         parent::startup();
  75.         $domain = $this->getParam('account');
  76.         $account = $this->context->sqldb->entityManager
  77.                                  ->getRepository('Ticket\Account\Entity\AccountEntity')
  78.                                  ->findOneByDomain($domain);        
  79.        
  80.         if (is_null($account)) {
  81.             throw new \Nette\InvalidArgumentException("Account with domain $domain was not found!");
  82.         }
  83.         // List all rpositories        
  84.         $em = $this->context->sqldb->entityManager;
  85.         /* @var $em \Doctrine\ORM\EntityManager */
  86.         foreach ($em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames() as $name) {
  87.             $repository = $em->getRepository($name);
  88.             if (\Nette\Reflection\ClassType::from($repository)->implementsInterface('Ticket\Account\Entity\Repository\IAccountableRepository')) {
  89.                 $repository->setAccount($account);
  90.             }
  91.         }        
  92.     }
  93.    
  94.     public function actionDefault()
  95.     {
  96.         $em = $this->context->sqldb->entityManager;
  97.         $ticket = $em->getRepository('Ticket\Ticket\Entity\TicketEntity')->createQueryBuilder('t')
  98.                 ->add('where', 't.id > 1')                
  99.                 //->setParameter('account', 1)
  100.                 ->getQuery()                    
  101.                 ->getResult();
  102.         \Nette\Diagnostics\Debugger::dump($ticket);
  103.         die();
  104.     }
  105.  
  106. }
Add Comment
Please, Sign In to add comment