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\Query;
23 * A ResultSetMapping describes how a result set of an SQL query maps to a Doctrine result.
26 * The properties of this class are only public for fast internal READ access and to (drastically)
27 * reduce the size of serialized instances for more effective caching due to better (un-)serialization
30 * <b>Users should use the public methods.</b>
32 * @author Roman Borschel <roman@code-factory.org>
34 * @todo Think about whether the number of lookup maps can be reduced.
36 class ResultSetMapping
40 * @var boolean Whether the result is mixed (contains scalar values together with field values).
42 public $isMixed = false;
46 * @var array Maps alias names to class names.
48 public $aliasMap = array();
52 * @var array Maps alias names to related association field names.
54 public $relationMap = array();
58 * @var array Maps alias names to parent alias names.
60 public $parentAliasMap = array();
64 * @var array Maps column names in the result set to field names for each class.
66 public $fieldMappings = array();
70 * @var array Maps column names in the result set to the alias/field name to use in the mapped result.
72 public $scalarMappings = array();
76 * @var array Maps column names in the result set to the alias/field type to use in the mapped result.
78 public $typeMappings = array();
82 * @var array Maps entities in the result set to the alias name to use in the mapped result.
84 public $entityMappings = array();
88 * @var array Maps column names of meta columns (foreign keys, discriminator columns, ...) to field names.
90 public $metaMappings = array();
94 * @var array Maps column names in the result set to the alias they belong to.
96 public $columnOwnerMap = array();
100 * @var array List of columns in the result set that are used as discriminator columns.
102 public $discriminatorColumns = array();
106 * @var array Maps alias names to field names that should be used for indexing.
108 public $indexByMap = array();
112 * @var array Map from column names to class names that declare the field the column is mapped to.
114 public $declaringClasses = array();
117 * @var array This is necessary to hydrate derivate foreign keys correctly.
119 public $isIdentifierColumn = array();
122 * Adds an entity result to this ResultSetMapping.
124 * @param string $class The class name of the entity.
125 * @param string $alias The alias for the class. The alias must be unique among all entity
126 * results or joined entity results within this ResultSetMapping.
127 * @param string $resultAlias The result alias with which the entity result should be
128 * placed in the result structure.
129 * @return ResultSetMapping This ResultSetMapping instance.
130 * @todo Rename: addRootEntity
132 public function addEntityResult($class, $alias, $resultAlias = null)
134 $this->aliasMap[$alias] = $class;
135 $this->entityMappings[$alias] = $resultAlias;
137 if ($resultAlias !== null) {
138 $this->isMixed = true;
145 * Sets a discriminator column for an entity result or joined entity result.
146 * The discriminator column will be used to determine the concrete class name to
149 * @param string $alias The alias of the entity result or joined entity result the discriminator
150 * column should be used for.
151 * @param string $discrColumn The name of the discriminator column in the SQL result set.
152 * @return ResultSetMapping This ResultSetMapping instance.
153 * @todo Rename: addDiscriminatorColumn
155 public function setDiscriminatorColumn($alias, $discrColumn)
157 $this->discriminatorColumns[$alias] = $discrColumn;
158 $this->columnOwnerMap[$discrColumn] = $alias;
164 * Sets a field to use for indexing an entity result or joined entity result.
166 * @param string $alias The alias of an entity result or joined entity result.
167 * @param string $fieldName The name of the field to use for indexing.
168 * @return ResultSetMapping This ResultSetMapping instance.
170 public function addIndexBy($alias, $fieldName)
174 foreach ($this->fieldMappings as $columnName => $columnFieldName) {
175 if ( ! ($columnFieldName === $fieldName && $this->columnOwnerMap[$columnName] === $alias)) continue;
177 $this->addIndexByColumn($alias, $columnName);
183 /* TODO: check if this exception can be put back, for now it's gone because of assumptions made by some ORM internals
186 'Cannot add index by for DQL alias %s and field %s without calling addFieldResult() for them before.',
191 throw new \LogicException($message);
199 * Set to index by a scalar result column name
201 * @param $resultColumnName
202 * @return ResultSetMapping This ResultSetMapping instance.
204 public function addIndexByScalar($resultColumnName)
206 $this->indexByMap['scalars'] = $resultColumnName;
212 * Sets a column to use for indexing an entity or joined entity result by the given alias name.
215 * @param $resultColumnName
216 * @return ResultSetMapping This ResultSetMapping instance.
218 public function addIndexByColumn($alias, $resultColumnName)
220 $this->indexByMap[$alias] = $resultColumnName;
226 * Checks whether an entity result or joined entity result with a given alias has
227 * a field set for indexing.
229 * @param string $alias
231 * @todo Rename: isIndexed($alias)
233 public function hasIndexBy($alias)
235 return isset($this->indexByMap[$alias]);
239 * Checks whether the column with the given name is mapped as a field result
240 * as part of an entity result or joined entity result.
242 * @param string $columnName The name of the column in the SQL result set.
244 * @todo Rename: isField
246 public function isFieldResult($columnName)
248 return isset($this->fieldMappings[$columnName]);
252 * Adds a field to the result that belongs to an entity or joined entity.
254 * @param string $alias The alias of the root entity or joined entity to which the field belongs.
255 * @param string $columnName The name of the column in the SQL result set.
256 * @param string $fieldName The name of the field on the declaring class.
257 * @param string $declaringClass The name of the class that declares/owns the specified field.
258 * When $alias refers to a superclass in a mapped hierarchy but
259 * the field $fieldName is defined on a subclass, specify that here.
260 * If not specified, the field is assumed to belong to the class
261 * designated by $alias.
262 * @return ResultSetMapping This ResultSetMapping instance.
263 * @todo Rename: addField
265 public function addFieldResult($alias, $columnName, $fieldName, $declaringClass = null)
267 // column name (in result set) => field name
268 $this->fieldMappings[$columnName] = $fieldName;
269 // column name => alias of owner
270 $this->columnOwnerMap[$columnName] = $alias;
271 // field name => class name of declaring class
272 $this->declaringClasses[$columnName] = $declaringClass ?: $this->aliasMap[$alias];
274 if ( ! $this->isMixed && $this->scalarMappings) {
275 $this->isMixed = true;
282 * Adds a joined entity result.
284 * @param string $class The class name of the joined entity.
285 * @param string $alias The unique alias to use for the joined entity.
286 * @param string $parentAlias The alias of the entity result that is the parent of this joined result.
287 * @param object $relation The association field that connects the parent entity result with the joined entity result.
288 * @return ResultSetMapping This ResultSetMapping instance.
289 * @todo Rename: addJoinedEntity
291 public function addJoinedEntityResult($class, $alias, $parentAlias, $relation)
293 $this->aliasMap[$alias] = $class;
294 $this->parentAliasMap[$alias] = $parentAlias;
295 $this->relationMap[$alias] = $relation;
301 * Adds a scalar result mapping.
303 * @param string $columnName The name of the column in the SQL result set.
304 * @param string $alias The result alias with which the scalar result should be placed in the result structure.
305 * @param string $type The column type
307 * @return ResultSetMapping This ResultSetMapping instance.
309 * @todo Rename: addScalar
311 public function addScalarResult($columnName, $alias, $type = 'string')
313 $this->scalarMappings[$columnName] = $alias;
314 $this->typeMappings[$columnName] = $type;
316 if ( ! $this->isMixed && $this->fieldMappings) {
317 $this->isMixed = true;
324 * Checks whether a column with a given name is mapped as a scalar result.
326 * @param string $columName The name of the column in the SQL result set.
328 * @todo Rename: isScalar
330 public function isScalarResult($columnName)
332 return isset($this->scalarMappings[$columnName]);
336 * Gets the name of the class of an entity result or joined entity result,
337 * identified by the given unique alias.
339 * @param string $alias
342 public function getClassName($alias)
344 return $this->aliasMap[$alias];
348 * Gets the field alias for a column that is mapped as a scalar value.
350 * @param string $columnName The name of the column in the SQL result set.
353 public function getScalarAlias($columnName)
355 return $this->scalarMappings[$columnName];
359 * Gets the name of the class that owns a field mapping for the specified column.
361 * @param string $columnName
364 public function getDeclaringClass($columnName)
366 return $this->declaringClasses[$columnName];
371 * @param string $alias
372 * @return AssociationMapping
374 public function getRelation($alias)
376 return $this->relationMap[$alias];
381 * @param string $alias
384 public function isRelation($alias)
386 return isset($this->relationMap[$alias]);
390 * Gets the alias of the class that owns a field mapping for the specified column.
392 * @param string $columnName
395 public function getEntityAlias($columnName)
397 return $this->columnOwnerMap[$columnName];
401 * Gets the parent alias of the given alias.
403 * @param string $alias
406 public function getParentAlias($alias)
408 return $this->parentAliasMap[$alias];
412 * Checks whether the given alias has a parent alias.
414 * @param string $alias
417 public function hasParentAlias($alias)
419 return isset($this->parentAliasMap[$alias]);
423 * Gets the field name for a column name.
425 * @param string $columnName
428 public function getFieldName($columnName)
430 return $this->fieldMappings[$columnName];
437 public function getAliasMap()
439 return $this->aliasMap;
443 * Gets the number of different entities that appear in the mapped result.
447 public function getEntityResultCount()
449 return count($this->aliasMap);
453 * Checks whether this ResultSetMapping defines a mixed result.
454 * Mixed results can only occur in object and array (graph) hydration. In such a
455 * case a mixed result means that scalar values are mixed with objects/array in
460 public function isMixedResult()
462 return $this->isMixed;
466 * Adds a meta column (foreign key or discriminator column) to the result set.
468 * @param string $alias
469 * @param string $columnName
470 * @param string $fieldName
472 * @return ResultSetMapping This ResultSetMapping instance.
474 public function addMetaResult($alias, $columnName, $fieldName, $isIdentifierColumn = false)
476 $this->metaMappings[$columnName] = $fieldName;
477 $this->columnOwnerMap[$columnName] = $alias;
479 if ($isIdentifierColumn) {
480 $this->isIdentifierColumn[$alias][$columnName] = true;