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.
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>.
20 namespace Doctrine\ORM;
22 use Doctrine\DBAL\LockMode;
23 use Doctrine\Common\Persistence\ObjectRepository;
25 use Doctrine\Common\Collections\Selectable;
26 use Doctrine\Common\Collections\Criteria;
27 use Doctrine\Common\Collections\ArrayCollection;
28 use Doctrine\Common\Collections\ExpressionBuilder;
31 * An EntityRepository serves as a repository for entities with generic as well as
32 * business specific methods for retrieving entities.
34 * This class is designed for inheritance and users can subclass this class to
35 * write their own repositories with business-specific methods to locate entities.
38 * @author Benjamin Eberlei <kontakt@beberlei.de>
39 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
40 * @author Jonathan Wage <jonwage@gmail.com>
41 * @author Roman Borschel <roman@code-factory.org>
43 class EntityRepository implements ObjectRepository, Selectable
48 protected $_entityName;
56 * @var \Doctrine\ORM\Mapping\ClassMetadata
61 * Initializes a new <tt>EntityRepository</tt>.
63 * @param EntityManager $em The EntityManager to use.
64 * @param ClassMetadata $classMetadata The class descriptor.
66 public function __construct($em, Mapping\ClassMetadata $class)
68 $this->_entityName = $class->name;
70 $this->_class = $class;
74 * Create a new QueryBuilder instance that is prepopulated for this entity name
76 * @param string $alias
77 * @return QueryBuilder $qb
79 public function createQueryBuilder($alias)
81 return $this->_em->createQueryBuilder()
83 ->from($this->_entityName, $alias);
87 * Create a new Query instance based on a predefined metadata named query.
89 * @param string $queryName
92 public function createNamedQuery($queryName)
94 return $this->_em->createQuery($this->_class->getNamedQuery($queryName));
98 * Creates a native SQL query.
100 * @param string $queryName
101 * @return NativeQuery
103 public function createNativeNamedQuery($queryName)
105 $queryMapping = $this->_class->getNamedNativeQuery($queryName);
106 $rsm = new Query\ResultSetMappingBuilder($this->_em);
107 $rsm->addNamedNativeQueryMapping($this->_class, $queryMapping);
109 return $this->_em->createNativeQuery($queryMapping['query'], $rsm);
113 * Clears the repository, causing all managed entities to become detached.
115 public function clear()
117 $this->_em->clear($this->_class->rootEntityName);
121 * Finds an entity by its primary key / identifier.
123 * @param mixed $id The identifier.
124 * @param integer $lockMode
125 * @param integer $lockVersion
127 * @return object The entity.
129 public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
131 return $this->_em->find($this->_entityName, $id, $lockMode, $lockVersion);
135 * Finds all entities in the repository.
137 * @return array The entities.
139 public function findAll()
141 return $this->findBy(array());
145 * Finds entities by a set of criteria.
147 * @param array $criteria
148 * @param array|null $orderBy
149 * @param int|null $limit
150 * @param int|null $offset
151 * @return array The objects.
153 public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
155 $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
157 return $persister->loadAll($criteria, $orderBy, $limit, $offset);
161 * Finds a single entity by a set of criteria.
163 * @param array $criteria
166 public function findOneBy(array $criteria)
168 $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
170 return $persister->load($criteria, null, null, array(), 0, 1);
174 * Adds support for magic finders.
176 * @return array|object The found entity/entities.
177 * @throws BadMethodCallException If the method called is an invalid find* method
178 * or no find* method at all and therefore an invalid
181 public function __call($method, $arguments)
184 case (0 === strpos($method, 'findBy')):
185 $by = substr($method, 6);
189 case (0 === strpos($method, 'findOneBy')):
190 $by = substr($method, 9);
191 $method = 'findOneBy';
195 throw new \BadMethodCallException(
196 "Undefined method '$method'. The method name must start with ".
197 "either findBy or findOneBy!"
201 if (empty($arguments)) {
202 throw ORMException::findByRequiresParameter($method . $by);
205 $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));
207 if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) {
208 switch (count($arguments)) {
210 return $this->$method(array($fieldName => $arguments[0]));
213 return $this->$method(array($fieldName => $arguments[0]), $arguments[1]);
216 return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]);
219 return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]);
226 throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
232 protected function getEntityName()
234 return $this->_entityName;
240 public function getClassName()
242 return $this->getEntityName();
246 * @return EntityManager
248 protected function getEntityManager()
254 * @return Mapping\ClassMetadata
256 protected function getClassMetadata()
258 return $this->_class;
262 * Select all elements from a selectable that match the expression and
263 * return a new collection containing these elements.
265 * @param \Doctrine\Common\Collections\Criteria $criteria
267 * @return \Doctrine\Common\Collections\Collection
269 public function matching(Criteria $criteria)
271 $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
273 return new ArrayCollection($persister->loadCriteria($criteria));