123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
-
-
- namespace Doctrine\ORM;
-
- use Doctrine\DBAL\LockMode;
- use Doctrine\Common\Persistence\ObjectRepository;
-
-
- class EntityRepository implements ObjectRepository
- {
-
-
- protected $_entityName;
-
-
-
- protected $_em;
-
-
-
- protected $_class;
-
-
-
- public function __construct($em, Mapping\ClassMetadata $class)
- {
- $this->_entityName = $class->name;
- $this->_em = $em;
- $this->_class = $class;
- }
-
-
-
- public function createQueryBuilder($alias)
- {
- return $this->_em->createQueryBuilder()
- ->select($alias)
- ->from($this->_entityName, $alias);
- }
-
-
-
- public function createNamedQuery($queryName)
- {
- return $this->_em->createQuery($this->_class->getNamedQuery($queryName));
- }
-
-
-
- public function clear()
- {
- $this->_em->clear($this->_class->rootEntityName);
- }
-
-
-
- public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
- {
-
- if ($entity = $this->_em->getUnitOfWork()->tryGetById($id, $this->_class->rootEntityName)) {
- if (!($entity instanceof $this->_class->name)) {
- return null;
- }
-
- if ($lockMode != LockMode::NONE) {
- $this->_em->lock($entity, $lockMode, $lockVersion);
- }
-
- return $entity;
- }
-
- if ( ! is_array($id) || count($id) <= 1) {
-
- $value = is_array($id) ? array_values($id) : array($id);
- $id = array_combine($this->_class->identifier, $value);
- }
-
- if ($lockMode == LockMode::NONE) {
- return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($id);
- } else if ($lockMode == LockMode::OPTIMISTIC) {
- if (!$this->_class->isVersioned) {
- throw OptimisticLockException::notVersioned($this->_entityName);
- }
- $entity = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($id);
-
- $this->_em->getUnitOfWork()->lock($entity, $lockMode, $lockVersion);
-
- return $entity;
- } else {
- if (!$this->_em->getConnection()->isTransactionActive()) {
- throw TransactionRequiredException::transactionRequired();
- }
-
- return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($id, null, null, array(), $lockMode);
- }
- }
-
-
-
- public function findAll()
- {
- return $this->findBy(array());
- }
-
-
-
- public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
- {
- return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria, $orderBy, $limit, $offset);
- }
-
-
-
- public function findOneBy(array $criteria)
- {
- return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($criteria);
- }
-
-
-
- public function __call($method, $arguments)
- {
- if (substr($method, 0, 6) == 'findBy') {
- $by = substr($method, 6, strlen($method));
- $method = 'findBy';
- } else if (substr($method, 0, 9) == 'findOneBy') {
- $by = substr($method, 9, strlen($method));
- $method = 'findOneBy';
- } else {
- throw new \BadMethodCallException(
- "Undefined method '$method'. The method name must start with ".
- "either findBy or findOneBy!"
- );
- }
-
- if ( !isset($arguments[0])) {
-
- throw ORMException::findByRequiresParameter($method.$by);
- }
-
- $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));
-
- if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) {
- return $this->$method(array($fieldName => $arguments[0]));
- } else {
- throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
- }
- }
-
-
-
- protected function getEntityName()
- {
- return $this->_entityName;
- }
-
-
-
- protected function getEntityManager()
- {
- return $this->_em;
- }
-
-
-
- protected function getClassMetadata()
- {
- return $this->_class;
- }
- }
|