EntityRepository.php 5.93 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18 19
 */

20
namespace Doctrine\ORM;
21

22
/**
23 24
 * An EntityRepository serves as a repository for entities with generic as well as
 * 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 30 31 32 33
 * @since   2.0
 * @author  Benjamin Eberlei <kontakt@beberlei.de>
 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author  Jonathan Wage <jonwage@gmail.com>
 * @author  Roman Borschel <roman@code-factory.org>
34
 */
35
class EntityRepository
36
{
37 38 39
    /**
     * @var string
     */
40
    protected $_entityName;
41 42 43 44

    /**
     * @var EntityManager
     */
45
    protected $_em;
46 47 48 49

    /**
     * @var Doctrine\ORM\Mapping\ClassMetadata
     */
50
    protected $_class;
51

52 53
    /**
     * Initializes a new <tt>EntityRepository</tt>.
54
     *
55 56 57
     * @param EntityManager $em The EntityManager to use.
     * @param ClassMetadata $classMetadata The class descriptor.
     */
58
    public function __construct($em, Mapping\ClassMetadata $class)
59
    {
60
        $this->_entityName = $class->name;
61
        $this->_em = $em;
62
        $this->_class = $class;
63
    }
64

65 66 67
    /**
     * Create a new QueryBuilder instance that is prepopulated for this entity name
     *
68
     * @param string $alias
69 70 71 72 73 74
     * @return QueryBuilder $qb
     */
    public function createQueryBuilder($alias)
    {
        return $this->_em->createQueryBuilder()
            ->select($alias)
75
            ->from($this->_entityName, $alias);
76
    }
77

78
    /**
79
     * Clears the repository, causing all managed entities to become detached.
80 81 82
     */
    public function clear()
    {
83
        $this->_em->clear($this->_class->rootEntityName);
84
    }
85

86
    /**
87
     * Finds an entity by its primary key / identifier.
88
     *
89 90
     * @param $id The identifier.
     * @param int $hydrationMode The hydration mode to use.
91
     * @return object The entity.
92
     */
93
    public function find($id)
94
    {
95
        // Check identity map first
96
        if ($entity = $this->_em->getUnitOfWork()->tryGetById($id, $this->_class->rootEntityName)) {
97 98
            return $entity; // Hit!
        }
99

100
        if ( ! is_array($id) || count($id) <= 1) {
101
            //FIXME: Not correct. Relies on specific order.
102
            $value = is_array($id) ? array_values($id) : array($id);
103
            $id = array_combine($this->_class->identifier, $value);
104 105
        }

106
        return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($id);
107 108 109
    }

    /**
110
     * Finds all entities in the repository.
111
     *
112
     * @param int $hydrationMode
113
     * @return array The entities.
114
     */
115
    public function findAll()
116
    {
117
        return $this->findBy(array());
118
    }
119

120
    /**
121
     * Finds entities by a set of criteria.
122
     *
123 124
     * @param string $column
     * @param string $value
125
     * @return array
126
     */
127
    public function findBy(array $criteria)
128
    {
129
        return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria);
130
    }
131

132
    /**
133
     * Finds a single entity by a set of criteria.
134
     *
135
     * @param string $column
136 137
     * @param string $value
     * @return object
138
     */
139
    public function findOneBy(array $criteria)
140
    {
141
        return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($criteria);
142
    }
143

144 145 146
    /**
     * Adds support for magic finders.
     *
147
     * @return array|object The found entity/entities.
romanb's avatar
romanb committed
148
     * @throws BadMethodCallException  If the method called is an invalid find* method
149 150
     *                                 or no find* method at all and therefore an invalid
     *                                 method call.
151 152 153 154 155 156 157 158 159 160
     */
    public function __call($method, $arguments)
    {
        if (substr($method, 0, 6) == 'findBy') {
            $by = substr($method, 6, strlen($method));
            $method = 'findBy';
        } else if (substr($method, 0, 9) == 'findOneBy') {
            $by = substr($method, 9, strlen($method));
            $method = 'findOneBy';
        } else {
161 162 163 164
            throw new \BadMethodCallException(
                "Undefined method '$method'. The method name must start with ".
                "either findBy or findOneBy!"
            );
165
        }
166

167
        if ( ! isset($arguments[0])) {
168
            throw ORMException::findByRequiresParameter($method.$by);
169 170 171 172 173 174 175
        }

        $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));

        if ($this->_class->hasField($fieldName)) {
            return $this->$method(array($fieldName => $arguments[0]));
        } else {
176
            throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
177 178
        }
    }
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    /**
     * @return string
     */
    protected function getEntityName()
    {
        return $this->_entityName;
    }

    /**
     * @return EntityManager
     */
    protected function getEntityManager()
    {
        return $this->_em;
    }

    /**
     * @return Mapping\ClassMetadata
     */
    protected function getClassMetadata()
    {
        return $this->_class;
    }
203
}