EntityRepository.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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\ORM\Query\ResultSetMappingBuilder;
  21. use Doctrine\DBAL\LockMode;
  22. use Doctrine\Common\Persistence\ObjectRepository;
  23. use Doctrine\Common\Collections\Selectable;
  24. use Doctrine\Common\Collections\Criteria;
  25. use Doctrine\Common\Collections\ArrayCollection;
  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 Mapping\ClassMetadata $class 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. * Creates a new QueryBuilder instance that is prepopulated for this entity name.
  67. *
  68. * @param string $alias
  69. *
  70. * @return QueryBuilder
  71. */
  72. public function createQueryBuilder($alias)
  73. {
  74. return $this->_em->createQueryBuilder()
  75. ->select($alias)
  76. ->from($this->_entityName, $alias);
  77. }
  78. /**
  79. * Creates a new result set mapping builder for this entity.
  80. *
  81. * The column naming strategy is "INCREMENT".
  82. *
  83. * @param string $alias
  84. *
  85. * @return ResultSetMappingBuilder
  86. */
  87. public function createResultSetMappingBuilder($alias)
  88. {
  89. $rsm = new ResultSetMappingBuilder($this->_em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT);
  90. $rsm->addRootEntityFromClassMetadata($this->_entityName, $alias);
  91. return $rsm;
  92. }
  93. /**
  94. * Creates a new Query instance based on a predefined metadata named query.
  95. *
  96. * @param string $queryName
  97. *
  98. * @return Query
  99. */
  100. public function createNamedQuery($queryName)
  101. {
  102. return $this->_em->createQuery($this->_class->getNamedQuery($queryName));
  103. }
  104. /**
  105. * Creates a native SQL query.
  106. *
  107. * @param string $queryName
  108. *
  109. * @return NativeQuery
  110. */
  111. public function createNativeNamedQuery($queryName)
  112. {
  113. $queryMapping = $this->_class->getNamedNativeQuery($queryName);
  114. $rsm = new Query\ResultSetMappingBuilder($this->_em);
  115. $rsm->addNamedNativeQueryMapping($this->_class, $queryMapping);
  116. return $this->_em->createNativeQuery($queryMapping['query'], $rsm);
  117. }
  118. /**
  119. * Clears the repository, causing all managed entities to become detached.
  120. *
  121. * @return void
  122. */
  123. public function clear()
  124. {
  125. $this->_em->clear($this->_class->rootEntityName);
  126. }
  127. /**
  128. * Finds an entity by its primary key / identifier.
  129. *
  130. * @param mixed $id The identifier.
  131. * @param int $lockMode The lock mode.
  132. * @param int|null $lockVersion The lock version.
  133. *
  134. * @return object|null The entity instance or NULL if the entity can not be found.
  135. */
  136. public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
  137. {
  138. return $this->_em->find($this->_entityName, $id, $lockMode, $lockVersion);
  139. }
  140. /**
  141. * Finds all entities in the repository.
  142. *
  143. * @return array The entities.
  144. */
  145. public function findAll()
  146. {
  147. return $this->findBy(array());
  148. }
  149. /**
  150. * Finds entities by a set of criteria.
  151. *
  152. * @param array $criteria
  153. * @param array|null $orderBy
  154. * @param int|null $limit
  155. * @param int|null $offset
  156. *
  157. * @return array The objects.
  158. */
  159. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  160. {
  161. $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
  162. return $persister->loadAll($criteria, $orderBy, $limit, $offset);
  163. }
  164. /**
  165. * Finds a single entity by a set of criteria.
  166. *
  167. * @param array $criteria
  168. * @param array|null $orderBy
  169. *
  170. * @return object|null The entity instance or NULL if the entity can not be found.
  171. */
  172. public function findOneBy(array $criteria, array $orderBy = null)
  173. {
  174. $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
  175. return $persister->load($criteria, null, null, array(), 0, 1, $orderBy);
  176. }
  177. /**
  178. * Adds support for magic finders.
  179. *
  180. * @param string $method
  181. * @param array $arguments
  182. *
  183. * @return array|object The found entity/entities.
  184. *
  185. * @throws ORMException
  186. * @throws \BadMethodCallException If the method called is an invalid find* method
  187. * or no find* method at all and therefore an invalid
  188. * method call.
  189. */
  190. public function __call($method, $arguments)
  191. {
  192. switch (true) {
  193. case (0 === strpos($method, 'findBy')):
  194. $by = substr($method, 6);
  195. $method = 'findBy';
  196. break;
  197. case (0 === strpos($method, 'findOneBy')):
  198. $by = substr($method, 9);
  199. $method = 'findOneBy';
  200. break;
  201. default:
  202. throw new \BadMethodCallException(
  203. "Undefined method '$method'. The method name must start with ".
  204. "either findBy or findOneBy!"
  205. );
  206. }
  207. if (empty($arguments)) {
  208. throw ORMException::findByRequiresParameter($method . $by);
  209. }
  210. $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));
  211. if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) {
  212. switch (count($arguments)) {
  213. case 1:
  214. return $this->$method(array($fieldName => $arguments[0]));
  215. case 2:
  216. return $this->$method(array($fieldName => $arguments[0]), $arguments[1]);
  217. case 3:
  218. return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]);
  219. case 4:
  220. return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]);
  221. default:
  222. // Do nothing
  223. }
  224. }
  225. throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
  226. }
  227. /**
  228. * @return string
  229. */
  230. protected function getEntityName()
  231. {
  232. return $this->_entityName;
  233. }
  234. /**
  235. * @return string
  236. */
  237. public function getClassName()
  238. {
  239. return $this->getEntityName();
  240. }
  241. /**
  242. * @return EntityManager
  243. */
  244. protected function getEntityManager()
  245. {
  246. return $this->_em;
  247. }
  248. /**
  249. * @return Mapping\ClassMetadata
  250. */
  251. protected function getClassMetadata()
  252. {
  253. return $this->_class;
  254. }
  255. /**
  256. * Select all elements from a selectable that match the expression and
  257. * return a new collection containing these elements.
  258. *
  259. * @param \Doctrine\Common\Collections\Criteria $criteria
  260. *
  261. * @return \Doctrine\Common\Collections\Collection
  262. */
  263. public function matching(Criteria $criteria)
  264. {
  265. $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
  266. return new ArrayCollection($persister->loadCriteria($criteria));
  267. }
  268. }