EntityRepository.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. if ($lockMode !== LockMode::NONE) {
  116. $this->_em->lock($entity, $lockMode, $lockVersion);
  117. }
  118. return $entity; // Hit!
  119. }
  120. switch ($lockMode) {
  121. case LockMode::NONE:
  122. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($sortedId);
  123. case LockMode::OPTIMISTIC:
  124. if ( ! $this->_class->isVersioned) {
  125. throw OptimisticLockException::notVersioned($this->_entityName);
  126. }
  127. $entity = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($sortedId);
  128. $this->_em->getUnitOfWork()->lock($entity, $lockMode, $lockVersion);
  129. return $entity;
  130. default:
  131. if ( ! $this->_em->getConnection()->isTransactionActive()) {
  132. throw TransactionRequiredException::transactionRequired();
  133. }
  134. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($sortedId, null, null, array(), $lockMode);
  135. }
  136. }
  137. /**
  138. * Finds all entities in the repository.
  139. *
  140. * @return array The entities.
  141. */
  142. public function findAll()
  143. {
  144. return $this->findBy(array());
  145. }
  146. /**
  147. * Finds entities by a set of criteria.
  148. *
  149. * @param array $criteria
  150. * @param array|null $orderBy
  151. * @param int|null $limit
  152. * @param int|null $offset
  153. * @return array The objects.
  154. */
  155. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  156. {
  157. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria, $orderBy, $limit, $offset);
  158. }
  159. /**
  160. * Finds a single entity by a set of criteria.
  161. *
  162. * @param array $criteria
  163. * @return object
  164. */
  165. public function findOneBy(array $criteria)
  166. {
  167. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($criteria, null, null, array(), 0, 1);
  168. }
  169. /**
  170. * Adds support for magic finders.
  171. *
  172. * @return array|object The found entity/entities.
  173. * @throws BadMethodCallException If the method called is an invalid find* method
  174. * or no find* method at all and therefore an invalid
  175. * method call.
  176. */
  177. public function __call($method, $arguments)
  178. {
  179. switch (true) {
  180. case (substr($method, 0, 6) == 'findBy'):
  181. $by = substr($method, 6, strlen($method));
  182. $method = 'findBy';
  183. break;
  184. case (substr($method, 0, 9) == 'findOneBy'):
  185. $by = substr($method, 9, strlen($method));
  186. $method = 'findOneBy';
  187. break;
  188. default:
  189. throw new \BadMethodCallException(
  190. "Undefined method '$method'. The method name must start with ".
  191. "either findBy or findOneBy!"
  192. );
  193. }
  194. if (empty($arguments)) {
  195. throw ORMException::findByRequiresParameter($method . $by);
  196. }
  197. $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));
  198. if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) {
  199. return $this->$method(array($fieldName => $arguments[0]));
  200. }
  201. throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
  202. }
  203. /**
  204. * @return string
  205. */
  206. protected function getEntityName()
  207. {
  208. return $this->_entityName;
  209. }
  210. /**
  211. * @return string
  212. */
  213. public function getClassName()
  214. {
  215. return $this->getEntityName();
  216. }
  217. /**
  218. * @return EntityManager
  219. */
  220. protected function getEntityManager()
  221. {
  222. return $this->_em;
  223. }
  224. /**
  225. * @return Mapping\ClassMetadata
  226. */
  227. protected function getClassMetadata()
  228. {
  229. return $this->_class;
  230. }
  231. }