AbsFunction.php
9 months ago
AvgFunction.php
9 months ago
BitAndFunction.php
9 months ago
BitOrFunction.php
9 months ago
ConcatFunction.php
9 months ago
CountFunction.php
9 months ago
CurrentDateFunction.php
9 months ago
CurrentTimeFunction.php
9 months ago
CurrentTimestampFunction.php
9 months ago
DateAddFunction.php
9 months ago
DateDiffFunction.php
9 months ago
DateSubFunction.php
9 months ago
FunctionNode.php
9 months ago
IdentityFunction.php
9 months ago
LengthFunction.php
9 months ago
LocateFunction.php
9 months ago
LowerFunction.php
9 months ago
MaxFunction.php
9 months ago
MinFunction.php
9 months ago
ModFunction.php
9 months ago
SizeFunction.php
9 months ago
SqrtFunction.php
9 months ago
SubstringFunction.php
9 months ago
SumFunction.php
9 months ago
TrimFunction.php
9 months ago
UpperFunction.php
9 months ago
index.php
9 months ago
IdentityFunction.php
65 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Query\AST\Functions; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\ORM\Query\AST\PathExpression; |
| 6 | use MailPoetVendor\Doctrine\ORM\Query\Parser; |
| 7 | use MailPoetVendor\Doctrine\ORM\Query\QueryException; |
| 8 | use MailPoetVendor\Doctrine\ORM\Query\SqlWalker; |
| 9 | use MailPoetVendor\Doctrine\ORM\Query\TokenType; |
| 10 | use function assert; |
| 11 | use function reset; |
| 12 | use function sprintf; |
| 13 | class IdentityFunction extends FunctionNode |
| 14 | { |
| 15 | public $pathExpression; |
| 16 | public $fieldMapping; |
| 17 | public function getSql(SqlWalker $sqlWalker) |
| 18 | { |
| 19 | assert($this->pathExpression->field !== null); |
| 20 | $entityManager = $sqlWalker->getEntityManager(); |
| 21 | $platform = $entityManager->getConnection()->getDatabasePlatform(); |
| 22 | $quoteStrategy = $entityManager->getConfiguration()->getQuoteStrategy(); |
| 23 | $dqlAlias = $this->pathExpression->identificationVariable; |
| 24 | $assocField = $this->pathExpression->field; |
| 25 | $assoc = $sqlWalker->getMetadataForDqlAlias($dqlAlias)->associationMappings[$assocField]; |
| 26 | $targetEntity = $entityManager->getClassMetadata($assoc['targetEntity']); |
| 27 | $joinColumn = reset($assoc['joinColumns']); |
| 28 | if ($this->fieldMapping !== null) { |
| 29 | if (!isset($targetEntity->fieldMappings[$this->fieldMapping])) { |
| 30 | throw new QueryException(sprintf('Undefined reference field mapping "%s"', $this->fieldMapping)); |
| 31 | } |
| 32 | $field = $targetEntity->fieldMappings[$this->fieldMapping]; |
| 33 | $joinColumn = null; |
| 34 | foreach ($assoc['joinColumns'] as $mapping) { |
| 35 | if ($mapping['referencedColumnName'] === $field['columnName']) { |
| 36 | $joinColumn = $mapping; |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | if ($joinColumn === null) { |
| 41 | throw new QueryException(sprintf('Unable to resolve the reference field mapping "%s"', $this->fieldMapping)); |
| 42 | } |
| 43 | } |
| 44 | // The table with the relation may be a subclass, so get the table name from the association definition |
| 45 | $tableName = $entityManager->getClassMetadata($assoc['sourceEntity'])->getTableName(); |
| 46 | $tableAlias = $sqlWalker->getSQLTableAlias($tableName, $dqlAlias); |
| 47 | $columnName = $quoteStrategy->getJoinColumnName($joinColumn, $targetEntity, $platform); |
| 48 | return $tableAlias . '.' . $columnName; |
| 49 | } |
| 50 | public function parse(Parser $parser) |
| 51 | { |
| 52 | $parser->match(TokenType::T_IDENTIFIER); |
| 53 | $parser->match(TokenType::T_OPEN_PARENTHESIS); |
| 54 | $this->pathExpression = $parser->SingleValuedAssociationPathExpression(); |
| 55 | if ($parser->getLexer()->isNextToken(TokenType::T_COMMA)) { |
| 56 | $parser->match(TokenType::T_COMMA); |
| 57 | $parser->match(TokenType::T_STRING); |
| 58 | $token = $parser->getLexer()->token; |
| 59 | assert($token !== null); |
| 60 | $this->fieldMapping = $token->value; |
| 61 | } |
| 62 | $parser->match(TokenType::T_CLOSE_PARENTHESIS); |
| 63 | } |
| 64 | } |
| 65 |