source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/ORM/EntityRepository.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 8.3 KB
Line 
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
20namespace Doctrine\ORM;
21
22use Doctrine\DBAL\LockMode;
23use Doctrine\Common\Persistence\ObjectRepository;
24
25/**
26 * An EntityRepository serves as a repository for entities with generic as well as
27 * business specific methods for retrieving entities.
28 *
29 * This class is designed for inheritance and users can subclass this class to
30 * write their own repositories with business-specific methods to locate entities.
31 *
32 * @since   2.0
33 * @author  Benjamin Eberlei <kontakt@beberlei.de>
34 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
35 * @author  Jonathan Wage <jonwage@gmail.com>
36 * @author  Roman Borschel <roman@code-factory.org>
37 */
38class EntityRepository implements ObjectRepository
39{
40    /**
41     * @var string
42     */
43    protected $_entityName;
44
45    /**
46     * @var EntityManager
47     */
48    protected $_em;
49
50    /**
51     * @var \Doctrine\ORM\Mapping\ClassMetadata
52     */
53    protected $_class;
54
55    /**
56     * Initializes a new <tt>EntityRepository</tt>.
57     *
58     * @param EntityManager $em The EntityManager to use.
59     * @param ClassMetadata $classMetadata The class descriptor.
60     */
61    public function __construct($em, Mapping\ClassMetadata $class)
62    {
63        $this->_entityName = $class->name;
64        $this->_em = $em;
65        $this->_class = $class;
66    }
67
68    /**
69     * Create a new QueryBuilder instance that is prepopulated for this entity name
70     *
71     * @param string $alias
72     * @return QueryBuilder $qb
73     */
74    public function createQueryBuilder($alias)
75    {
76        return $this->_em->createQueryBuilder()
77            ->select($alias)
78            ->from($this->_entityName, $alias);
79    }
80
81    /**
82     * Create a new Query instance based on a predefined metadata named query.
83     *
84     * @param string $queryName
85     * @return Query
86     */
87    public function createNamedQuery($queryName)
88    {
89        return $this->_em->createQuery($this->_class->getNamedQuery($queryName));
90    }
91
92    /**
93     * Clears the repository, causing all managed entities to become detached.
94     */
95    public function clear()
96    {
97        $this->_em->clear($this->_class->rootEntityName);
98    }
99
100    /**
101     * Finds an entity by its primary key / identifier.
102     *
103     * @param $id The identifier.
104     * @param int $lockMode
105     * @param int $lockVersion
106     * @return object The entity.
107     */
108    public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
109    {
110        if ( ! is_array($id)) {
111            $id = array($this->_class->identifier[0] => $id);
112        }
113        $sortedId = array();
114        foreach ($this->_class->identifier as $identifier) {
115            if (!isset($id[$identifier])) {
116                throw ORMException::missingIdentifierField($this->_class->name, $identifier);
117            }
118            $sortedId[$identifier] = $id[$identifier];
119        }
120
121        // Check identity map first
122        if ($entity = $this->_em->getUnitOfWork()->tryGetById($sortedId, $this->_class->rootEntityName)) {
123            if ( ! ($entity instanceof $this->_class->name)) {
124                return null;
125            }
126
127            switch ($lockMode) {
128                case LockMode::OPTIMISTIC:
129                    $this->_em->lock($entity, $lockMode, $lockVersion);
130                    break;
131                case LockMode::PESSIMISTIC_READ:
132                case LockMode::PESSIMISTIC_WRITE:
133                    $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
134                    $persister->refresh($sortedId, $entity, $lockMode);
135                    break;
136            }
137
138            return $entity; // Hit!
139        }
140
141        switch ($lockMode) {
142            case LockMode::NONE:
143                return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($sortedId);
144
145            case LockMode::OPTIMISTIC:
146                if ( ! $this->_class->isVersioned) {
147                    throw OptimisticLockException::notVersioned($this->_entityName);
148                }
149
150                $entity = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($sortedId);
151
152                $this->_em->getUnitOfWork()->lock($entity, $lockMode, $lockVersion);
153
154                return $entity;
155
156            default:
157                if ( ! $this->_em->getConnection()->isTransactionActive()) {
158                    throw TransactionRequiredException::transactionRequired();
159                }
160
161                return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($sortedId, null, null, array(), $lockMode);
162        }
163    }
164
165    /**
166     * Finds all entities in the repository.
167     *
168     * @return array The entities.
169     */
170    public function findAll()
171    {
172        return $this->findBy(array());
173    }
174
175    /**
176     * Finds entities by a set of criteria.
177     *
178     * @param array $criteria
179     * @param array|null $orderBy
180     * @param int|null $limit
181     * @param int|null $offset
182     * @return array The objects.
183     */
184    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
185    {
186        return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria, $orderBy, $limit, $offset);
187    }
188
189    /**
190     * Finds a single entity by a set of criteria.
191     *
192     * @param array $criteria
193     * @return object
194     */
195    public function findOneBy(array $criteria)
196    {
197        return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($criteria, null, null, array(), 0, 1);
198    }
199
200    /**
201     * Adds support for magic finders.
202     *
203     * @return array|object The found entity/entities.
204     * @throws BadMethodCallException  If the method called is an invalid find* method
205     *                                 or no find* method at all and therefore an invalid
206     *                                 method call.
207     */
208    public function __call($method, $arguments)
209    {
210        switch (true) {
211            case (substr($method, 0, 6) == 'findBy'):
212                $by = substr($method, 6, strlen($method));
213                $method = 'findBy';
214                break;
215
216            case (substr($method, 0, 9) == 'findOneBy'):
217                $by = substr($method, 9, strlen($method));
218                $method = 'findOneBy';
219                break;
220
221            default:
222                throw new \BadMethodCallException(
223                    "Undefined method '$method'. The method name must start with ".
224                    "either findBy or findOneBy!"
225                );
226        }
227
228        if (empty($arguments)) {
229            throw ORMException::findByRequiresParameter($method . $by);
230        }
231
232        $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));
233
234        if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) {
235            return $this->$method(array($fieldName => $arguments[0]));
236        }
237
238        throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
239    }
240
241    /**
242     * @return string
243     */
244    protected function getEntityName()
245    {
246        return $this->_entityName;
247    }
248
249    /**
250     * @return string
251     */
252    public function getClassName()
253    {
254        return $this->getEntityName();
255    }
256
257    /**
258     * @return EntityManager
259     */
260    protected function getEntityManager()
261    {
262        return $this->_em;
263    }
264
265    /**
266     * @return Mapping\ClassMetadata
267     */
268    protected function getClassMetadata()
269    {
270        return $this->_class;
271    }
272}
Note: See TracBrowser for help on using the repository browser.