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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
|
<?php
declare(strict_types=1);
namespace Doctrine\ORM\Query;
use function array_merge;
use function count;
/**
* A ResultSetMapping describes how a result set of an SQL query maps to a Doctrine result.
*
* IMPORTANT NOTE:
* 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.
*
* <b>Users should use the public methods.</b>
*
* @todo Think about whether the number of lookup maps can be reduced.
*/
class ResultSetMapping
{
/**
* Whether the result is mixed (contains scalar values together with field values).
*
* @ignore
* @var bool
*/
public $isMixed = false;
/**
* Whether the result is a select statement.
*
* @ignore
* @var bool
*/
public $isSelect = true;
/**
* Maps alias names to class names.
*
* @ignore
* @psalm-var array<string, class-string>
*/
public $aliasMap = [];
/**
* Maps alias names to related association field names.
*
* @ignore
* @psalm-var array<string, string>
*/
public $relationMap = [];
/**
* Maps alias names to parent alias names.
*
* @ignore
* @psalm-var array<string, string>
*/
public $parentAliasMap = [];
/**
* Maps column names in the result set to field names for each class.
*
* @ignore
* @psalm-var array<string, string>
*/
public $fieldMappings = [];
/**
* Maps column names in the result set to the alias/field name to use in the mapped result.
*
* @ignore
* @psalm-var array<string, string|int>
*/
public $scalarMappings = [];
/**
* Maps scalar columns to enums
*
* @ignore
* @psalm-var array<string, string>
*/
public $enumMappings = [];
/**
* Maps column names in the result set to the alias/field type to use in the mapped result.
*
* @ignore
* @psalm-var array<string, string>
*/
public $typeMappings = [];
/**
* Maps entities in the result set to the alias name to use in the mapped result.
*
* @ignore
* @psalm-var array<string, string|null>
*/
public $entityMappings = [];
/**
* Maps column names of meta columns (foreign keys, discriminator columns, ...) to field names.
*
* @ignore
* @psalm-var array<string, string>
*/
public $metaMappings = [];
/**
* Maps column names in the result set to the alias they belong to.
*
* @ignore
* @psalm-var array<string, string>
*/
public $columnOwnerMap = [];
/**
* List of columns in the result set that are used as discriminator columns.
*
* @ignore
* @psalm-var array<string, string>
*/
public $discriminatorColumns = [];
/**
* Maps alias names to field names that should be used for indexing.
*
* @ignore
* @psalm-var array<string, string>
*/
public $indexByMap = [];
/**
* Map from column names to class names that declare the field the column is mapped to.
*
* @ignore
* @psalm-var array<string, class-string>
*/
public $declaringClasses = [];
/**
* This is necessary to hydrate derivate foreign keys correctly.
*
* @psalm-var array<string, array<string, bool>>
*/
public $isIdentifierColumn = [];
/**
* Maps column names in the result set to field names for each new object expression.
*
* @psalm-var array<string, array<string, mixed>>
*/
public $newObjectMappings = [];
/**
* Maps metadata parameter names to the metadata attribute.
*
* @psalm-var array<int|string, string>
*/
public $metadataParameterMapping = [];
/**
* Contains query parameter names to be resolved as discriminator values
*
* @psalm-var array<string, string>
*/
public $discriminatorParameters = [];
/**
* Adds an entity result to this ResultSetMapping.
*
* @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.
* @param string|null $resultAlias The result alias with which the entity result should be
* placed in the result structure.
* @psalm-param class-string $class
*
* @return $this
*
* @todo Rename: addRootEntity
*/
public function addEntityResult($class, $alias, $resultAlias = null)
{
$this->aliasMap[$alias] = $class;
$this->entityMappings[$alias] = $resultAlias;
if ($resultAlias !== null) {
$this->isMixed = true;
}
return $this;
}
/**
* 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.
*
* @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.
*
* @return $this
*
* @todo Rename: addDiscriminatorColumn
*/
public function setDiscriminatorColumn($alias, $discrColumn)
{
$this->discriminatorColumns[$alias] = $discrColumn;
$this->columnOwnerMap[$discrColumn] = $alias;
return $this;
}
/**
* Sets a field to use for indexing an entity result or joined entity result.
*
* @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.
*
* @return $this
*/
public function addIndexBy($alias, $fieldName)
{
$found = false;
foreach (array_merge($this->metaMappings, $this->fieldMappings) as $columnName => $columnFieldName) {
if (! ($columnFieldName === $fieldName && $this->columnOwnerMap[$columnName] === $alias)) {
continue;
}
$this->addIndexByColumn($alias, $columnName);
$found = true;
break;
}
/* TODO: check if this exception can be put back, for now it's gone because of assumptions made by some ORM internals
if ( ! $found) {
$message = sprintf(
'Cannot add index by for DQL alias %s and field %s without calling addFieldResult() for them before.',
$alias,
$fieldName
);
throw new \LogicException($message);
}
*/
return $this;
}
/**
* Sets to index by a scalar result column name.
*
* @param string $resultColumnName
*
* @return $this
*/
public function addIndexByScalar($resultColumnName)
{
$this->indexByMap['scalars'] = $resultColumnName;
return $this;
}
/**
* Sets a column to use for indexing an entity or joined entity result by the given alias name.
*
* @param string $alias
* @param string $resultColumnName
*
* @return $this
*/
public function addIndexByColumn($alias, $resultColumnName)
{
$this->indexByMap[$alias] = $resultColumnName;
return $this;
}
/**
* Checks whether an entity result or joined entity result with a given alias has
* a field set for indexing.
*
* @param string $alias
*
* @return bool
*
* @todo Rename: isIndexed($alias)
*/
public function hasIndexBy($alias)
{
return isset($this->indexByMap[$alias]);
}
/**
* Checks whether the column with the given name is mapped as a field result
* as part of an entity result or joined entity result.
*
* @param string $columnName The name of the column in the SQL result set.
*
* @return bool
*
* @todo Rename: isField
*/
public function isFieldResult($columnName)
{
return isset($this->fieldMappings[$columnName]);
}
/**
* Adds a field to the result that belongs to an entity or joined entity.
*
* @param string $alias The alias of the root entity or joined entity to which the field belongs.
* @param string $columnName The name of the column in the SQL result set.
* @param string $fieldName The name of the field on the declaring class.
* @param string|null $declaringClass The name of the class that declares/owns the specified field.
* When $alias refers to a superclass in a mapped hierarchy but
* the field $fieldName is defined on a subclass, specify that here.
* If not specified, the field is assumed to belong to the class
* designated by $alias.
* @psalm-param class-string|null $declaringClass
*
* @return $this
*
* @todo Rename: addField
*/
public function addFieldResult($alias, $columnName, $fieldName, $declaringClass = null)
{
// column name (in result set) => field name
$this->fieldMappings[$columnName] = $fieldName;
// column name => alias of owner
$this->columnOwnerMap[$columnName] = $alias;
// field name => class name of declaring class
$this->declaringClasses[$columnName] = $declaringClass ?: $this->aliasMap[$alias];
if (! $this->isMixed && $this->scalarMappings) {
$this->isMixed = true;
}
return $this;
}
/**
* Adds a joined entity result.
*
* @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.
* @param string $relation The association field that connects the parent entity result
* with the joined entity result.
* @psalm-param class-string $class
*
* @return $this
*
* @todo Rename: addJoinedEntity
*/
public function addJoinedEntityResult($class, $alias, $parentAlias, $relation)
{
$this->aliasMap[$alias] = $class;
$this->parentAliasMap[$alias] = $parentAlias;
$this->relationMap[$alias] = $relation;
return $this;
}
/**
* Adds a scalar result mapping.
*
* @param string $columnName The name of the column in the SQL result set.
* @param string|int $alias The result alias with which the scalar result should be placed in the result structure.
* @param string $type The column type
*
* @return $this
*
* @todo Rename: addScalar
*/
public function addScalarResult($columnName, $alias, $type = 'string')
{
$this->scalarMappings[$columnName] = $alias;
$this->typeMappings[$columnName] = $type;
if (! $this->isMixed && $this->fieldMappings) {
$this->isMixed = true;
}
return $this;
}
/**
* Adds a scalar result mapping.
*
* @param string $columnName The name of the column in the SQL result set.
* @param string $enumType The enum type
*
* @return $this
*/
public function addEnumResult($columnName, $enumType)
{
$this->enumMappings[$columnName] = $enumType;
return $this;
}
/**
* Adds a metadata parameter mappings.
*
* @param string|int $parameter The parameter name in the SQL result set.
* @param string $attribute The metadata attribute.
*
* @return void
*/
public function addMetadataParameterMapping($parameter, $attribute)
{
$this->metadataParameterMapping[$parameter] = $attribute;
}
/**
* Checks whether a column with a given name is mapped as a scalar result.
*
* @param string $columnName The name of the column in the SQL result set.
*
* @return bool
*
* @todo Rename: isScalar
*/
public function isScalarResult($columnName)
{
return isset($this->scalarMappings[$columnName]);
}
/**
* Gets the name of the class of an entity result or joined entity result,
* identified by the given unique alias.
*
* @param string $alias
*
* @return class-string
*/
public function getClassName($alias)
{
return $this->aliasMap[$alias];
}
/**
* Gets the field alias for a column that is mapped as a scalar value.
*
* @param string $columnName The name of the column in the SQL result set.
*
* @return string|int
*/
public function getScalarAlias($columnName)
{
return $this->scalarMappings[$columnName];
}
/**
* Gets the name of the class that owns a field mapping for the specified column.
*
* @param string $columnName
*
* @return class-string
*/
public function getDeclaringClass($columnName)
{
return $this->declaringClasses[$columnName];
}
/**
* @param string $alias
*
* @return string
*/
public function getRelation($alias)
{
return $this->relationMap[$alias];
}
/**
* @param string $alias
*
* @return bool
*/
public function isRelation($alias)
{
return isset($this->relationMap[$alias]);
}
/**
* Gets the alias of the class that owns a field mapping for the specified column.
*
* @param string $columnName
*
* @return string
*/
public function getEntityAlias($columnName)
{
return $this->columnOwnerMap[$columnName];
}
/**
* Gets the parent alias of the given alias.
*
* @param string $alias
*
* @return string
*/
public function getParentAlias($alias)
{
return $this->parentAliasMap[$alias];
}
/**
* Checks whether the given alias has a parent alias.
*
* @param string $alias
*
* @return bool
*/
public function hasParentAlias($alias)
{
return isset($this->parentAliasMap[$alias]);
}
/**
* Gets the field name for a column name.
*
* @param string $columnName
*
* @return string
*/
public function getFieldName($columnName)
{
return $this->fieldMappings[$columnName];
}
/** @psalm-return array<string, class-string> */
public function getAliasMap()
{
return $this->aliasMap;
}
/**
* Gets the number of different entities that appear in the mapped result.
*
* @return int
* @psalm-return 0|positive-int
*/
public function getEntityResultCount()
{
return count($this->aliasMap);
}
/**
* 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.
*
* @return bool
*/
public function isMixedResult()
{
return $this->isMixed;
}
/**
* Adds a meta column (foreign key or discriminator column) to the result set.
*
* @param string $alias The result alias with which the meta result should be placed in the result structure.
* @param string $columnName The name of the column in the SQL result set.
* @param string $fieldName The name of the field on the declaring class.
* @param bool $isIdentifierColumn
* @param string|null $type The column type
*
* @return $this
*
* @todo Make all methods of this class require all parameters and not infer anything
*/
public function addMetaResult($alias, $columnName, $fieldName, $isIdentifierColumn = false, $type = null)
{
$this->metaMappings[$columnName] = $fieldName;
$this->columnOwnerMap[$columnName] = $alias;
if ($isIdentifierColumn) {
$this->isIdentifierColumn[$alias][$columnName] = true;
}
if ($type) {
$this->typeMappings[$columnName] = $type;
}
return $this;
}
}
|