EntityRepository.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // Check identity map first
  101. if ($entity = $this->_em->getUnitOfWork()->tryGetById($id, $this->_class->rootEntityName)) {
  102. if (!($entity instanceof $this->_class->name)) {
  103. return null;
  104. }
  105. if ($lockMode != LockMode::NONE) {
  106. $this->_em->lock($entity, $lockMode, $lockVersion);
  107. }
  108. return $entity; // Hit!
  109. }
  110. if ( ! is_array($id) || count($id) <= 1) {
  111. // @todo FIXME: Not correct. Relies on specific order.
  112. $value = is_array($id) ? array_values($id) : array($id);
  113. $id = array_combine($this->_class->identifier, $value);
  114. }
  115. if ($lockMode == LockMode::NONE) {
  116. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($id);
  117. } else if ($lockMode == LockMode::OPTIMISTIC) {
  118. if (!$this->_class->isVersioned) {
  119. throw OptimisticLockException::notVersioned($this->_entityName);
  120. }
  121. $entity = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($id);
  122. $this->_em->getUnitOfWork()->lock($entity, $lockMode, $lockVersion);
  123. return $entity;
  124. } else {
  125. if (!$this->_em->getConnection()->isTransactionActive()) {
  126. throw TransactionRequiredException::transactionRequired();
  127. }
  128. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($id, null, null, array(), $lockMode);
  129. }
  130. }
  131. /**
  132. * Finds all entities in the repository.
  133. *
  134. * @return array The entities.
  135. */
  136. public function findAll()
  137. {
  138. return $this->findBy(array());
  139. }
  140. /**
  141. * Finds entities by a set of criteria.
  142. *
  143. * @param array $criteria
  144. * @param array|null $orderBy
  145. * @param int|null $limit
  146. * @param int|null $offset
  147. * @return array The objects.
  148. */
  149. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  150. {
  151. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria, $orderBy, $limit, $offset);
  152. }
  153. /**
  154. * Finds a single entity by a set of criteria.
  155. *
  156. * @param array $criteria
  157. * @return object
  158. */
  159. public function findOneBy(array $criteria)
  160. {
  161. return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($criteria);
  162. }
  163. /**
  164. * Adds support for magic finders.
  165. *
  166. * @return array|object The found entity/entities.
  167. * @throws BadMethodCallException If the method called is an invalid find* method
  168. * or no find* method at all and therefore an invalid
  169. * method call.
  170. */
  171. public function __call($method, $arguments)
  172. {
  173. if (substr($method, 0, 6) == 'findBy') {
  174. $by = substr($method, 6, strlen($method));
  175. $method = 'findBy';
  176. } else if (substr($method, 0, 9) == 'findOneBy') {
  177. $by = substr($method, 9, strlen($method));
  178. $method = 'findOneBy';
  179. } else {
  180. throw new \BadMethodCallException(
  181. "Undefined method '$method'. The method name must start with ".
  182. "either findBy or findOneBy!"
  183. );
  184. }
  185. if ( !isset($arguments[0])) {
  186. // we dont even want to allow null at this point, because we cannot (yet) transform it into IS NULL.
  187. throw ORMException::findByRequiresParameter($method.$by);
  188. }
  189. $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));
  190. if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) {
  191. return $this->$method(array($fieldName => $arguments[0]));
  192. } else {
  193. throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
  194. }
  195. }
  196. /**
  197. * @return string
  198. */
  199. protected function getEntityName()
  200. {
  201. return $this->_entityName;
  202. }
  203. /**
  204. * @return EntityManager
  205. */
  206. protected function getEntityManager()
  207. {
  208. return $this->_em;
  209. }
  210. /**
  211. * @return Mapping\ClassMetadata
  212. */
  213. protected function getClassMetadata()
  214. {
  215. return $this->_class;
  216. }
  217. }