Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- namespace Ticket\Account\Entity\Repository;
- use Ticket;
- use Signifymedia;
- use Doctrine;
- class AccoutableRepository extends Doctrine\ORM\EntityRepository implements IAccountableRepository
- {
- /** @var Ticket\Account\Entity\AccountEntity */
- protected $actualAccount = null;
- public function setAccount(\Ticket\Account\Entity\AccountEntity $account)
- {
- $this->actualAccount = $account;
- }
- public function createQueryBuilder($alias)
- {
- $q = parent::createQueryBuilder($alias);
- if (is_null($this->actualAccount)) {
- return $q;
- }
- return $q->add("where", "$alias.account = :account")
- ->setParameter('account', $this->actualAccount->getId());
- }
- public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
- {
- if (!is_null($this->actualAccount)) {
- $criteria['account'] = $this->actualAccount->getId();
- }
- return parent::findBy($criteria, $orderBy, $limit, $offset);
- }
- public function findOneBy(array $criteria)
- {
- $criteria = array();
- if (!is_null($this->actualAccount)) {
- $criteria['account'] = $this->actualAccount->getId();
- }
- return parent::findOneBy($criteria);
- }
- public function find($id, $lockMode = Doctrine\DBAL\LockMode::NONE, $lockVersion = null)
- {
- $entity = parent::find($id, $lockMode, $lockVersion);
- if (!is_null($entity)) {
- $accountFiled = \Nette\Reflection\ClassType::from($entity)->getProperty('account');
- $accountFiled->setAccessible(true);
- if ($accountFiled->getValue($entity)->getId() !== $this->actualAccount->getId()) {
- $this->_em->detach($entity);
- $entity = null;
- }
- }
- return $entity;
- }
- }
- abstract class BasePresenter extends \Nette\Application\UI\Presenter
- {
- protected function startup()
- {
- parent::startup();
- $domain = $this->getParam('account');
- $account = $this->context->sqldb->entityManager
- ->getRepository('Ticket\Account\Entity\AccountEntity')
- ->findOneByDomain($domain);
- if (is_null($account)) {
- throw new \Nette\InvalidArgumentException("Account with domain $domain was not found!");
- }
- // List all rpositories
- $em = $this->context->sqldb->entityManager;
- /* @var $em \Doctrine\ORM\EntityManager */
- foreach ($em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames() as $name) {
- $repository = $em->getRepository($name);
- if (\Nette\Reflection\ClassType::from($repository)->implementsInterface('Ticket\Account\Entity\Repository\IAccountableRepository')) {
- $repository->setAccount($account);
- }
- }
- }
- public function actionDefault()
- {
- $em = $this->context->sqldb->entityManager;
- $ticket = $em->getRepository('Ticket\Ticket\Entity\TicketEntity')->createQueryBuilder('t')
- ->add('where', 't.id > 1')
- //->setParameter('account', 1)
- ->getQuery()
- ->getResult();
- \Nette\Diagnostics\Debugger::dump($ticket);
- die();
- }
- }
Add Comment
Please, Sign In to add comment