ResultSetMapping.php 9.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?php
/*
 *  $Id$
 *
 * 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
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\ORM\Query;

/**
 * A ResultSetMapping describes how a result set of an SQL query maps to a Doctrine result.
 *
27
 * IMPORTANT NOTE:
28 29 30 31
 * The properties of this class are only public for fast internal READ access and to (drastically)
 * reduce the size of serialized instances for more effective caching due to better (un-)serialization
 * performance.
 * 
32
 * <b>Users should use the public methods.</b>
33
 *
34 35 36 37 38
 * @author Roman Borschel <roman@code-factory.org>
 * @since 2.0
 */
class ResultSetMapping
{
39
    /** Whether the result is mixed (contains scalar values together with field values). */
40
    public $isMixed = false;
41
    /** Maps alias names to ClassMetadata descriptors. */
42
    public $aliasMap = array();
43
    /** Maps alias names to related association field names. */
44
    public $relationMap = array();
45
    /** Maps alias names to parent alias names. */
46
    public $parentAliasMap = array();
47
    /** Maps column names in the result set to field names for each class. */
48
    public $fieldMappings = array();
49
    /** Maps column names in the result set to the alias to use in the mapped result. */
50
    public $scalarMappings = array();
51 52
    /** Maps column names of meta columns (foreign keys, discriminator columns, ...) to field names. */
    public $metaMappings = array();
53
    /** Maps column names in the result set to the alias they belong to. */
54
    public $columnOwnerMap = array();
55
    /** List of columns in the result set that are used as discriminator columns. */
56
    public $discriminatorColumns = array();
57
    /** Maps alias names to field names that should be used for indexing. */
58
    public $indexByMap = array();
59 60

    /**
61
     * Adds an entity result to this ResultSetMapping.
62
     *
63 64 65
     * @param string $class The class name of the entity.
     * @param string $alias The alias for the class. The alias must be unique among all entity
     *                      results or joined entity results within this ResultSetMapping.
66
     */
67
    public function addEntityResult($class, $alias)
68
    {
69
        $this->aliasMap[$alias] = $class;
70 71
    }

72
    /**
73 74 75
     * Sets a discriminator column for an entity result or joined entity result.
     * The discriminator column will be used to determine the concrete class name to
     * instantiate.
76
     *
77 78 79
     * @param string $alias The alias of the entity result or joined entity result the discriminator
     *                      column should be used for.
     * @param string $discrColumn The name of the discriminator column in the SQL result set.
80
     */
81
    public function setDiscriminatorColumn($alias, $discrColumn)
82
    {
83
        $this->discriminatorColumns[$alias] = $discrColumn;
84
        $this->columnOwnerMap[$discrColumn] = $alias;
85 86
    }

87
    /**
88
     * Sets a field to use for indexing an entity result or joined entity result.
89
     *
90 91
     * @param string $alias The alias of an entity result or joined entity result.
     * @param string $fieldName The name of the field to use for indexing.
92
     */
93 94
    public function addIndexBy($alias, $fieldName)
    {
95
        $this->indexByMap[$alias] = $fieldName;
96 97
    }

98
    /**
99 100
     * Checks whether an entity result or joined entity result with a given alias has
     * a field set for indexing.
101 102 103 104
     *
     * @param string $alias
     * @return boolean
     */
105 106
    public function hasIndexBy($alias)
    {
107
        return isset($this->indexByMap[$alias]);
108 109
    }

110
    /**
111 112
     * Checks whether the column with the given name is mapped as a field result
     * as part of an entity result or joined entity result.
113
     *
114
     * @param string $columnName The name of the column in the SQL result set.
115 116
     * @return boolean
     */
117 118
    public function isFieldResult($columnName)
    {
119
        return isset($this->fieldMappings[$columnName]);
120 121
    }

122
    /**
123
     * Adds a field result that is part of an entity result or joined entity result.
124
     *
125 126 127
     * @param string $alias The alias of the entity result or joined entity result.
     * @param string $columnName The name of the column in the SQL result set.
     * @param string $fieldName The name of the field on the (joined) entity.
128
     */
129 130
    public function addFieldResult($alias, $columnName, $fieldName)
    {
131 132 133 134
        $this->fieldMappings[$columnName] = $fieldName;
        $this->columnOwnerMap[$columnName] = $alias;
        if ( ! $this->isMixed && $this->scalarMappings) {
            $this->isMixed = true;
135
        }
136 137
    }

138
    /**
139
     * Adds a joined entity result.
140
     *
141 142 143
     * @param string $class The class name of the joined entity.
     * @param string $alias The unique alias to use for the joined entity.
     * @param string $parentAlias The alias of the entity result that is the parent of this joined result.
144
     * @param object $relation The association field that connects the parent entity result with the joined entity result.
145
     */
146
    public function addJoinedEntityResult($class, $alias, $parentAlias, $relation)
147
    {
148 149 150
        $this->aliasMap[$alias] = $class;
        $this->parentAliasMap[$alias] = $parentAlias;
        $this->relationMap[$alias] = $relation;
151
    }
152 153
    
    /**
154
     * Adds a scalar result mapping.
155
     *
156
     * @param string $columnName The name of the column in the SQL result set.
157
     * @param string $alias The result alias with which the scalar result should be placed in the result structure.
158
     */
159 160
    public function addScalarResult($columnName, $alias)
    {
161 162 163
        $this->scalarMappings[$columnName] = $alias;
        if ( ! $this->isMixed && $this->fieldMappings) {
            $this->isMixed = true;
164
        }
romanb's avatar
romanb committed
165
    }
166 167

    /**
168 169 170
     * Checks whether a column with a given name is mapped as a scalar result.
     * 
     * @param string $columName The name of the column in the SQL result set.
171 172 173 174
     * @return boolean
     */
    public function isScalarResult($columnName)
    {
175
        return isset($this->scalarMappings[$columnName]);
176 177 178
    }

    /**
179 180
     * Gets the name of the class of an entity result or joined entity result,
     * identified by the given unique alias.
181
     *
182 183
     * @param string $alias
     * @return string
184
     */
185
    public function getClassName($alias)
186
    {
187
        return $this->aliasMap[$alias];
188 189 190
    }

    /**
191
     * Gets the field alias for a column that is mapped as a scalar value.
192
     *
193
     * @param string $columnName The name of the column in the SQL result set.
194 195 196 197
     * @return string
     */
    public function getScalarAlias($columnName)
    {
198
        return $this->scalarMappings[$columnName];
199 200 201
    }

    /**
202
     * Gets the name of the class that owns a field mapping for the specified column.
203 204
     *
     * @param string $columnName
205
     * @return string
206 207 208
     */
    public function getOwningClass($columnName)
    {
209
        return $this->aliasMap[$this->columnOwnerMap[$columnName]];
210 211
    }

212 213 214 215 216
    /**
     *
     * @param string $alias
     * @return AssociationMapping
     */
217 218
    public function getRelation($alias)
    {
219
        return $this->relationMap[$alias];
220 221
    }

222 223 224 225 226
    /**
     *
     * @param string $alias
     * @return boolean
     */
227 228
    public function isRelation($alias)
    {
229
        return isset($this->relationMap[$alias]);
230 231
    }

232
    /**
233
     * Gets the alias of the class that owns a field mapping for the specified column.
234
     *
235 236
     * @param string $columnName
     * @return string
237 238 239
     */
    public function getEntityAlias($columnName)
    {
240
        return $this->columnOwnerMap[$columnName];
241 242 243
    }

    /**
244
     * Gets the parent alias of the given alias.
245
     *
246 247
     * @param string $alias
     * @return string
248 249 250
     */
    public function getParentAlias($alias)
    {
251
        return $this->parentAliasMap[$alias];
252 253
    }

254
    /**
255
     * Checks whether the given alias has a parent alias.
256 257 258 259
     *
     * @param string $alias
     * @return boolean
     */
260 261
    public function hasParentAlias($alias)
    {
262
        return isset($this->parentAliasMap[$alias]);
263 264 265
    }

    /**
266
     * Gets the field name for a column name.
267
     *
268 269
     * @param string $columnName
     * @return string
270 271 272
     */
    public function getFieldName($columnName)
    {
273
        return $this->fieldMappings[$columnName];
274 275
    }

276 277 278 279
    /**
     *
     * @return array
     */
280 281
    public function getAliasMap()
    {
282
        return $this->aliasMap;
283 284
    }

285
    /**
286
     * Gets the number of different entities that appear in the mapped result.
287 288 289
     *
     * @return integer
     */
290 291
    public function getEntityResultCount()
    {
292
        return count($this->aliasMap);
293
    }
294

295
    /**
296 297 298 299
     * Checks whether this ResultSetMapping defines a mixed result.
     * Mixed results can only occur in object and array (graph) hydration. In such a
     * case a mixed result means that scalar values are mixed with objects/array in
     * the result.
300 301 302
     *
     * @return boolean
     */
303 304
    public function isMixedResult()
    {
305
        return $this->isMixed;
306
    }
307 308 309 310 311 312 313 314 315 316 317 318 319
    
    /**
     * 
     * @param $alias
     * @param $columnName
     * @param $fieldName
     * @return unknown_type
     */
    public function addMetaResult($alias, $columnName, $fieldName)
    {
        $this->metaMappings[$columnName] = $fieldName;
        $this->columnOwnerMap[$columnName] = $alias;
    }
320 321
}