EntityRepository.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM;
  20. use Doctrine\DBAL\LockMode;
  21. use Doctrine\Common\Persistence\ObjectRepository;
  22. /**
  23. * An EntityRepository serves as a repository for entities with generic as well as
  24. * business specific methods for retrieving entities.
  25. *
  26. * This class is designed for inheritance and users can subclass this class to
  27. * write their own repositories with business-specific methods to locate entities.
  28. *
  29. * @since 2.0
  30. * @author Benjamin Eberlei <kontakt@beberlei.de>
  31. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  32. * @author Jonathan Wage <jonwage@gmail.com>
  33. * @author Roman Borschel <roman@code-factory.org>
  34. */
  35. class EntityRepository implements ObjectRepository
  36. {
  37. /**
  38. * @var string
  39. */
  40. protected $_entityName;
  41. /**
  42. * @var EntityManager
  43. */
  44. protected $_em;
  45. /**
  46. * @var \Doctrine\ORM\Mapping\ClassMetadata
  47. */
  48. protected $_class;
  49. /**
  50. * Initializes a new <tt>EntityRepository</tt>.
  51. *
  52. * @param EntityManager $em The EntityManager to use.
  53. * @param ClassMetadata $classMetadata The class descriptor.
  54. */
  55. public function __construct($em, Mapping\ClassMetadata $class)
  56. {
  57. $this->_entityName = $class->name;
  58. $this->_em = $em;
  59. $this->_class = $class;
  60. }
  61. /**
  62. * Create a new QueryBuilder instance that is prepopulated for this entity name
  63. *
  64. * @param string $alias
  65. * @return QueryBuilder $qb
  66. */
  67. public function createQueryBuilder($alias)
  68. {
  69. return $this->_em->createQueryBuilder()
  70. ->select($alias)
  71. ->from($this->_entityName, $alias);
  72. }
  73. /**
  74. * Create a new Query instance based on a predefined metadata named query.
  75. *
  76. * @param string $queryName
  77. * @return Query
  78. */
  79. public function createNamedQuery($queryName)
  80. {
  81. return $this->_em->createQuery($this->_class->getNamedQuery($queryName));
  82. }
  83. /**
  84. * Clears the repository, causing all managed entities to become detached.
  85. */
  86. public function clear()
  87. {
  88. $this->_em->clear($this->_class->rootEntityName);
  89. }
  90. /**
  91. * Finds an entity by its primary key / identifier.
  92. *
  93. * @param $id The identifier.
  94. * @param int $lockMode
  95. * @param int $lockVersion
  96. * @return object The entity.
  97. */
  98. public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
  99. {
  100. if ( ! is_array($id)) {
  101. $id = array($this->_class->identifier[0] => $id);
  102. }
  103. $sortedId = array();
  104. foreach ($this->_class->identifier as $identifier) {
  105. if (!isset($id[$identifier])) {
  106. throw ORMException::missingIdentifierField($this->_class->name, $identifier);
  107. }
  108. $sortedId[$identifier] = $id[$identifier];
  109. }
  110. // Check identity map first
  111. if ($entity = $this->_em->getUnitOfWork()->tryGetById($sortedId, $this->_class->rootEntityName)) {
  112. if ( ! ($entity instanceof $this->_class->name)) {
  113. return null;
  114. }
  115. switch ($lockMode) {
  116. case LockMode::OPTIMISTIC:
  117. $this->_em->lock($entity, $lockMode, $lockVersion);
  118. break;
  119. case LockMode::PESSIMISTIC_READ:
  120. case LockMode::PESSIMISTIC_WRITE:
  121. $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
  122. $persister->refresh($sortedId, $entity, $lockMode);
  123. break;
  124. }
  125. return $entity; // Hit!
  126. }
  127. switch ($lockMode) {
  128. case LockMode::NONE:
  129. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($sortedId);
  130. case LockMode::OPTIMISTIC:
  131. if ( ! $this->_class->isVersioned) {
  132. throw OptimisticLockException::notVersioned($this->_entityName);
  133. }
  134. $entity = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($sortedId);
  135. $this->_em->getUnitOfWork()->lock($entity, $lockMode, $lockVersion);
  136. return $entity;
  137. default:
  138. if ( ! $this->_em->getConnection()->isTransactionActive()) {
  139. throw TransactionRequiredException::transactionRequired();
  140. }
  141. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($sortedId, null, null, array(), $lockMode);
  142. }
  143. }
  144. /**
  145. * Finds all entities in the repository.
  146. *
  147. * @return array The entities.
  148. */
  149. public function findAll()
  150. {
  151. return $this->findBy(array());
  152. }
  153. /**
  154. * Finds entities by a set of criteria.
  155. *
  156. * @param array $criteria
  157. * @param array|null $orderBy
  158. * @param int|null $limit
  159. * @param int|null $offset
  160. * @return array The objects.
  161. */
  162. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  163. {
  164. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria, $orderBy, $limit, $offset);
  165. }
  166. /**
  167. * Finds a single entity by a set of criteria.
  168. *
  169. * @param array $criteria
  170. * @return object
  171. */
  172. public function findOneBy(array $criteria)
  173. {
  174. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($criteria, null, null, array(), 0, 1);
  175. }
  176. /**
  177. * Adds support for magic finders.
  178. *
  179. * @return array|object The found entity/entities.
  180. * @throws BadMethodCallException If the method called is an invalid find* method
  181. * or no find* method at all and therefore an invalid
  182. * method call.
  183. */
  184. public function __call($method, $arguments)
  185. {
  186. switch (true) {
  187. case (substr($method, 0, 6) == 'findBy'):
  188. $by = substr($method, 6, strlen($method));
  189. $method = 'findBy';
  190. break;
  191. case (substr($method, 0, 9) == 'findOneBy'):
  192. $by = substr($method, 9, strlen($method));
  193. $method = 'findOneBy';
  194. break;
  195. default:
  196. throw new \BadMethodCallException(
  197. "Undefined method '$method'. The method name must start with ".
  198. "either findBy or findOneBy!"
  199. );
  200. }
  201. if (empty($arguments)) {
  202. throw ORMException::findByRequiresParameter($method . $by);
  203. }
  204. $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));
  205. if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) {
  206. return $this->$method(array($fieldName => $arguments[0]));
  207. }
  208. throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
  209. }
  210. /**
  211. * @return string
  212. */
  213. protected function getEntityName()
  214. {
  215. return $this->_entityName;
  216. }
  217. /**
  218. * @return string
  219. */
  220. public function getClassName()
  221. {
  222. return $this->getEntityName();
  223. }
  224. /**
  225. * @return EntityManager
  226. */
  227. protected function getEntityManager()
  228. {
  229. return $this->_em;
  230. }
  231. /**
  232. * @return Mapping\ClassMetadata
  233. */
  234. protected function getClassMetadata()
  235. {
  236. return $this->_class;
  237. }
  238. }