EntityRepository.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 MIT license. 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. use Doctrine\Common\Collections\Selectable;
  23. use Doctrine\Common\Collections\Criteria;
  24. use Doctrine\Common\Collections\ArrayCollection;
  25. use Doctrine\Common\Collections\ExpressionBuilder;
  26. /**
  27. * An EntityRepository serves as a repository for entities with generic as well as
  28. * business specific methods for retrieving entities.
  29. *
  30. * This class is designed for inheritance and users can subclass this class to
  31. * write their own repositories with business-specific methods to locate entities.
  32. *
  33. * @since 2.0
  34. * @author Benjamin Eberlei <kontakt@beberlei.de>
  35. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  36. * @author Jonathan Wage <jonwage@gmail.com>
  37. * @author Roman Borschel <roman@code-factory.org>
  38. */
  39. class EntityRepository implements ObjectRepository, Selectable
  40. {
  41. /**
  42. * @var string
  43. */
  44. protected $_entityName;
  45. /**
  46. * @var EntityManager
  47. */
  48. protected $_em;
  49. /**
  50. * @var \Doctrine\ORM\Mapping\ClassMetadata
  51. */
  52. protected $_class;
  53. /**
  54. * Initializes a new <tt>EntityRepository</tt>.
  55. *
  56. * @param EntityManager $em The EntityManager to use.
  57. * @param ClassMetadata $classMetadata The class descriptor.
  58. */
  59. public function __construct($em, Mapping\ClassMetadata $class)
  60. {
  61. $this->_entityName = $class->name;
  62. $this->_em = $em;
  63. $this->_class = $class;
  64. }
  65. /**
  66. * Create a new QueryBuilder instance that is prepopulated for this entity name
  67. *
  68. * @param string $alias
  69. * @return QueryBuilder $qb
  70. */
  71. public function createQueryBuilder($alias)
  72. {
  73. return $this->_em->createQueryBuilder()
  74. ->select($alias)
  75. ->from($this->_entityName, $alias);
  76. }
  77. /**
  78. * Create a new Query instance based on a predefined metadata named query.
  79. *
  80. * @param string $queryName
  81. * @return Query
  82. */
  83. public function createNamedQuery($queryName)
  84. {
  85. return $this->_em->createQuery($this->_class->getNamedQuery($queryName));
  86. }
  87. /**
  88. * Creates a native SQL query.
  89. *
  90. * @param string $queryName
  91. * @return NativeQuery
  92. */
  93. public function createNativeNamedQuery($queryName)
  94. {
  95. $queryMapping = $this->_class->getNamedNativeQuery($queryName);
  96. $rsm = new Query\ResultSetMappingBuilder($this->_em);
  97. $rsm->addNamedNativeQueryMapping($this->_class, $queryMapping);
  98. return $this->_em->createNativeQuery($queryMapping['query'], $rsm);
  99. }
  100. /**
  101. * Clears the repository, causing all managed entities to become detached.
  102. */
  103. public function clear()
  104. {
  105. $this->_em->clear($this->_class->rootEntityName);
  106. }
  107. /**
  108. * Finds an entity by its primary key / identifier.
  109. *
  110. * @param mixed $id The identifier.
  111. * @param integer $lockMode
  112. * @param integer $lockVersion
  113. *
  114. * @return object The entity.
  115. */
  116. public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
  117. {
  118. return $this->_em->find($this->_entityName, $id, $lockMode, $lockVersion);
  119. }
  120. /**
  121. * Finds all entities in the repository.
  122. *
  123. * @return array The entities.
  124. */
  125. public function findAll()
  126. {
  127. return $this->findBy(array());
  128. }
  129. /**
  130. * Finds entities by a set of criteria.
  131. *
  132. * @param array $criteria
  133. * @param array|null $orderBy
  134. * @param int|null $limit
  135. * @param int|null $offset
  136. * @return array The objects.
  137. */
  138. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  139. {
  140. $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
  141. return $persister->loadAll($criteria, $orderBy, $limit, $offset);
  142. }
  143. /**
  144. * Finds a single entity by a set of criteria.
  145. *
  146. * @param array $criteria
  147. * @param array|null $orderBy
  148. * @return object
  149. */
  150. public function findOneBy(array $criteria, array $orderBy = null)
  151. {
  152. $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
  153. return $persister->load($criteria, null, null, array(), 0, 1, $orderBy);
  154. }
  155. /**
  156. * Adds support for magic finders.
  157. *
  158. * @return array|object The found entity/entities.
  159. * @throws BadMethodCallException If the method called is an invalid find* method
  160. * or no find* method at all and therefore an invalid
  161. * method call.
  162. */
  163. public function __call($method, $arguments)
  164. {
  165. switch (true) {
  166. case (0 === strpos($method, 'findBy')):
  167. $by = substr($method, 6);
  168. $method = 'findBy';
  169. break;
  170. case (0 === strpos($method, 'findOneBy')):
  171. $by = substr($method, 9);
  172. $method = 'findOneBy';
  173. break;
  174. default:
  175. throw new \BadMethodCallException(
  176. "Undefined method '$method'. The method name must start with ".
  177. "either findBy or findOneBy!"
  178. );
  179. }
  180. if (empty($arguments)) {
  181. throw ORMException::findByRequiresParameter($method . $by);
  182. }
  183. $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));
  184. if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) {
  185. switch (count($arguments)) {
  186. case 1:
  187. return $this->$method(array($fieldName => $arguments[0]));
  188. case 2:
  189. return $this->$method(array($fieldName => $arguments[0]), $arguments[1]);
  190. case 3:
  191. return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]);
  192. case 4:
  193. return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]);
  194. default:
  195. // Do nothing
  196. }
  197. }
  198. throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
  199. }
  200. /**
  201. * @return string
  202. */
  203. protected function getEntityName()
  204. {
  205. return $this->_entityName;
  206. }
  207. /**
  208. * @return string
  209. */
  210. public function getClassName()
  211. {
  212. return $this->getEntityName();
  213. }
  214. /**
  215. * @return EntityManager
  216. */
  217. protected function getEntityManager()
  218. {
  219. return $this->_em;
  220. }
  221. /**
  222. * @return Mapping\ClassMetadata
  223. */
  224. protected function getClassMetadata()
  225. {
  226. return $this->_class;
  227. }
  228. /**
  229. * Select all elements from a selectable that match the expression and
  230. * return a new collection containing these elements.
  231. *
  232. * @param \Doctrine\Common\Collections\Criteria $criteria
  233. *
  234. * @return \Doctrine\Common\Collections\Collection
  235. */
  236. public function matching(Criteria $criteria)
  237. {
  238. $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
  239. return new ArrayCollection($persister->loadCriteria($criteria));
  240. }
  241. }