vendor/shopware/core/Framework/DataAbstractionLayer/Dbal/EntityDefinitionQueryHelper.php line 530

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Dbal;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Exception\UnmappedFieldException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\FieldResolver\FieldResolverContext;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Field\AssociationField;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Inherited;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Field\ReferenceVersionField;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Field\StorageAware;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Field\TranslatedField;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Field\VersionField;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\CriteriaPartInterface;
  22. use Shopware\Core\Framework\Feature;
  23. use Shopware\Core\Framework\Uuid\Uuid;
  24. /**
  25.  * This class acts only as helper/common class for all dbal operations for entity definitions.
  26.  * It knows how an association should be joined, how a parent-child inheritance should act, how translation chains work, ...
  27.  *
  28.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  29.  */
  30. class EntityDefinitionQueryHelper
  31. {
  32.     public const HAS_TO_MANY_JOIN 'has_to_many_join';
  33.     public static function escape(string $string): string
  34.     {
  35.         if (mb_strpos($string'`') !== false) {
  36.             throw new \InvalidArgumentException('Backtick not allowed in identifier');
  37.         }
  38.         return '`' $string '`';
  39.     }
  40.     public static function columnExists(Connection $connectionstring $tablestring $column): bool
  41.     {
  42.         $exists $connection->fetchOne(
  43.             'SHOW COLUMNS FROM ' self::escape($table) . ' WHERE `Field` LIKE :column',
  44.             ['column' => $column]
  45.         );
  46.         return !empty($exists);
  47.     }
  48.     public static function getFieldsOfAccessor(EntityDefinition $definitionstring $accessorbool $resolveTranslated true): array
  49.     {
  50.         $parts explode('.'$accessor);
  51.         if ($definition->getEntityName() === $parts[0]) {
  52.             array_shift($parts);
  53.         }
  54.         $accessorFields = [];
  55.         $source $definition;
  56.         foreach ($parts as $part) {
  57.             if ($part === 'extensions') {
  58.                 continue;
  59.             }
  60.             $fields $source->getFields();
  61.             $field $fields->get($part);
  62.             // continue if the current part is not a real field to allow access on collections
  63.             if (!$field) {
  64.                 continue;
  65.             }
  66.             if ($field instanceof TranslatedField && $resolveTranslated) {
  67.                 $source $source->getTranslationDefinition();
  68.                 $fields $source->getFields();
  69.                 $accessorFields[] = $fields->get($part);
  70.                 continue;
  71.             }
  72.             if ($field instanceof TranslatedField && !$resolveTranslated) {
  73.                 $accessorFields[] = $field;
  74.                 continue;
  75.             }
  76.             $accessorFields[] = $field;
  77.             if (!$field instanceof AssociationField) {
  78.                 break;
  79.             }
  80.             $source $field->getReferenceDefinition();
  81.             if ($field instanceof ManyToManyAssociationField) {
  82.                 $source $field->getToManyReferenceDefinition();
  83.             }
  84.         }
  85.         return array_filter($accessorFields);
  86.     }
  87.     /**
  88.      * Returns the field instance of the provided fieldName.
  89.      *
  90.      * @example
  91.      *
  92.      * fieldName => 'product.name'
  93.      * Returns the (new TranslatedField('name')) declaration
  94.      *
  95.      * Allows additionally nested referencing
  96.      *
  97.      * fieldName => 'category.products.name'
  98.      * Returns as well the above field definition
  99.      */
  100.     public function getField(string $fieldNameEntityDefinition $definitionstring $rootbool $resolveTranslated true): ?Field
  101.     {
  102.         $original $fieldName;
  103.         $prefix $root '.';
  104.         if (mb_strpos($fieldName$prefix) === 0) {
  105.             $fieldName mb_substr($fieldNamemb_strlen($prefix));
  106.         } else {
  107.             $original $prefix $original;
  108.         }
  109.         $fields $definition->getFields();
  110.         $isAssociation mb_strpos($fieldName'.') !== false;
  111.         if (!$isAssociation && $fields->has($fieldName)) {
  112.             return $fields->get($fieldName);
  113.         }
  114.         $associationKey explode('.'$fieldName);
  115.         $associationKey array_shift($associationKey);
  116.         $field $fields->get($associationKey);
  117.         if ($field instanceof TranslatedField && $resolveTranslated) {
  118.             return self::getTranslatedField($definition$field);
  119.         }
  120.         if ($field instanceof TranslatedField) {
  121.             return $field;
  122.         }
  123.         if (!$field instanceof AssociationField) {
  124.             return $field;
  125.         }
  126.         $referenceDefinition $field->getReferenceDefinition();
  127.         if ($field instanceof ManyToManyAssociationField) {
  128.             $referenceDefinition $field->getToManyReferenceDefinition();
  129.         }
  130.         return $this->getField(
  131.             $original,
  132.             $referenceDefinition,
  133.             $root '.' $field->getPropertyName()
  134.         );
  135.     }
  136.     /**
  137.      * Builds the sql field accessor for the provided field.
  138.      *
  139.      * @example
  140.      *
  141.      * fieldName => product.taxId
  142.      * root      => product
  143.      * returns   => `product`.`tax_id`
  144.      *
  145.      * This function is also used for complex field accessors like JsonArray Field, JsonObject fields.
  146.      * It considers the translation and parent-child inheritance.
  147.      *
  148.      * fieldName => product.name
  149.      * root      => product
  150.      * return    => COALESCE(`product.translation`.`name`,`product.parent.translation`.`name`)
  151.      *
  152.      * @throws UnmappedFieldException
  153.      */
  154.     public function getFieldAccessor(string $fieldNameEntityDefinition $definitionstring $rootContext $context): string
  155.     {
  156.         $fieldName str_replace('extensions.'''$fieldName);
  157.         $original $fieldName;
  158.         $prefix $root '.';
  159.         if (mb_strpos($fieldName$prefix) === 0) {
  160.             $fieldName mb_substr($fieldNamemb_strlen($prefix));
  161.         } else {
  162.             $original $prefix $original;
  163.         }
  164.         $fields $definition->getFields();
  165.         if ($fields->has($fieldName)) {
  166.             $field $fields->get($fieldName);
  167.             return $this->buildInheritedAccessor($field$root$definition$context$fieldName);
  168.         }
  169.         $parts explode('.'$fieldName);
  170.         $associationKey array_shift($parts);
  171.         if ($associationKey === 'extensions') {
  172.             $associationKey array_shift($parts);
  173.         }
  174.         if (!$fields->has($associationKey)) {
  175.             throw new UnmappedFieldException($original$definition);
  176.         }
  177.         $field $fields->get($associationKey);
  178.         //case for json object fields, other fields has now same option to act with more point notations but hasn't to be an association field. E.g. price.gross
  179.         if (!$field instanceof AssociationField && ($field instanceof StorageAware || $field instanceof TranslatedField)) {
  180.             return $this->buildInheritedAccessor($field$root$definition$context$fieldName);
  181.         }
  182.         if (!$field instanceof AssociationField) {
  183.             throw new \RuntimeException(sprintf('Expected field "%s" to be instance of %s'$associationKeyAssociationField::class));
  184.         }
  185.         $referenceDefinition $field->getReferenceDefinition();
  186.         if ($field instanceof ManyToManyAssociationField) {
  187.             $referenceDefinition $field->getToManyReferenceDefinition();
  188.         }
  189.         return $this->getFieldAccessor(
  190.             $original,
  191.             $referenceDefinition,
  192.             $root '.' $field->getPropertyName(),
  193.             $context
  194.         );
  195.     }
  196.     public static function getAssociationPath(string $accessorEntityDefinition $definition): ?string
  197.     {
  198.         $fields self::getFieldsOfAccessor($definition$accessor);
  199.         $path = [];
  200.         foreach ($fields as $field) {
  201.             if (!$field instanceof AssociationField) {
  202.                 break;
  203.             }
  204.             $path[] = $field->getPropertyName();
  205.         }
  206.         if (empty($path)) {
  207.             return null;
  208.         }
  209.         return implode('.'$path);
  210.     }
  211.     /**
  212.      * Creates the basic root query for the provided entity definition and application context.
  213.      * It considers the current context version.
  214.      */
  215.     public function getBaseQuery(QueryBuilder $queryEntityDefinition $definitionContext $context): QueryBuilder
  216.     {
  217.         $table $definition->getEntityName();
  218.         $query->from(self::escape($table));
  219.         $useVersionFallback // only applies for versioned entities
  220.             $definition->isVersionAware()
  221.             // only add live fallback if the current version isn't the live version
  222.             && $context->getVersionId() !== Defaults::LIVE_VERSION
  223.             // sub entities have no live fallback
  224.             && $definition->getParentDefinition() === null;
  225.         if ($useVersionFallback) {
  226.             $this->joinVersion($query$definition$definition->getEntityName(), $context);
  227.         } elseif ($definition->isVersionAware()) {
  228.             $versionIdField array_filter(
  229.                 $definition->getPrimaryKeys()->getElements(),
  230.                 function ($f) {
  231.                     return $f instanceof VersionField || $f instanceof ReferenceVersionField;
  232.                 }
  233.             );
  234.             if (!$versionIdField) {
  235.                 throw new \RuntimeException('Missing `VersionField` in `' $definition->getClass() . '`');
  236.             }
  237.             /** @var FkField|null $versionIdField */
  238.             $versionIdField array_shift($versionIdField);
  239.             $query->andWhere(self::escape($table) . '.' self::escape($versionIdField->getStorageName()) . ' = :version');
  240.             $query->setParameter('version'Uuid::fromHexToBytes($context->getVersionId()));
  241.         }
  242.         return $query;
  243.     }
  244.     /**
  245.      * Used for dynamic sql joins. In case that the given fieldName is unknown or event nested with multiple association
  246.      * roots, the function can resolve each association part of the field name, even if one part of the fieldName contains a translation or event inherited data field.
  247.      */
  248.     public function resolveAccessor(
  249.         string $accessor,
  250.         EntityDefinition $definition,
  251.         string $root,
  252.         QueryBuilder $query,
  253.         Context $context,
  254.         ?CriteriaPartInterface $criteriaPart null
  255.     ): void {
  256.         $accessor str_replace('extensions.'''$accessor);
  257.         $parts explode('.'$accessor);
  258.         if ($parts[0] === $root) {
  259.             unset($parts[0]);
  260.         }
  261.         $alias $root;
  262.         $path = [$root];
  263.         $rootDefinition $definition;
  264.         foreach ($parts as $part) {
  265.             $field $definition->getFields()->get($part);
  266.             if ($field === null) {
  267.                 return;
  268.             }
  269.             $resolver $field->getResolver();
  270.             if ($resolver === null) {
  271.                 continue;
  272.             }
  273.             if ($field instanceof AssociationField) {
  274.                 $path[] = $field->getPropertyName();
  275.             }
  276.             $currentPath implode('.'$path);
  277.             $resolverContext = new FieldResolverContext($currentPath$alias$field$definition$rootDefinition$query$context$criteriaPart);
  278.             $alias $this->callResolver($resolverContext);
  279.             if (!$field instanceof AssociationField) {
  280.                 return;
  281.             }
  282.             $definition $field->getReferenceDefinition();
  283.             if ($field instanceof ManyToManyAssociationField) {
  284.                 $definition $field->getToManyReferenceDefinition();
  285.             }
  286.             if ($definition->isInheritanceAware() && $context->considerInheritance() && $parent $definition->getField('parent')) {
  287.                 $resolverContext = new FieldResolverContext($currentPath$alias$parent$definition$rootDefinition$query$context$criteriaPart);
  288.                 $this->callResolver($resolverContext);
  289.             }
  290.         }
  291.     }
  292.     public function resolveField(Field $fieldEntityDefinition $definitionstring $rootQueryBuilder $queryContext $context): void
  293.     {
  294.         $resolver $field->getResolver();
  295.         if ($resolver === null) {
  296.             return;
  297.         }
  298.         $resolver->join(new FieldResolverContext($root$root$field$definition$definition$query$contextnull));
  299.     }
  300.     /**
  301.      * Adds the full translation select part to the provided sql query.
  302.      * Considers the parent-child inheritance and provided context language inheritance.
  303.      * The raw parameter allows to skip the parent-child inheritance.
  304.      */
  305.     public function addTranslationSelect(string $rootEntityDefinition $definitionQueryBuilder $queryContext $context, array $partial = []): void
  306.     {
  307.         $translationDefinition $definition->getTranslationDefinition();
  308.         if (!$translationDefinition) {
  309.             return;
  310.         }
  311.         $fields $translationDefinition->getFields();
  312.         if (!empty($partial)) {
  313.             $fields $translationDefinition->getFields()->filter(function (Field $field) use ($partial) {
  314.                 return $field->is(PrimaryKey::class)
  315.                     || isset($partial[$field->getPropertyName()])
  316.                     || $field instanceof FkField;
  317.             });
  318.         }
  319.         $inherited $context->considerInheritance() && $definition->isInheritanceAware();
  320.         $chain EntityDefinitionQueryHelper::buildTranslationChain($root$context$inherited);
  321.         /** @var TranslatedField $field */
  322.         foreach ($fields as $field) {
  323.             if (!$field instanceof StorageAware) {
  324.                 continue;
  325.             }
  326.             $selects = [];
  327.             foreach ($chain as $select) {
  328.                 $vars = [
  329.                     '#root#' => $select,
  330.                     '#field#' => $field->getPropertyName(),
  331.                 ];
  332.                 $query->addSelect(str_replace(
  333.                     array_keys($vars),
  334.                     array_values($vars),
  335.                     EntityDefinitionQueryHelper::escape('#root#.#field#')
  336.                 ));
  337.                 $selects[] = str_replace(
  338.                     array_keys($vars),
  339.                     array_values($vars),
  340.                     self::escape('#root#.#field#')
  341.                 );
  342.             }
  343.             //check if current field is a translated field of the origin definition
  344.             $origin $definition->getFields()->get($field->getPropertyName());
  345.             if (!$origin instanceof TranslatedField) {
  346.                 continue;
  347.             }
  348.             $selects[] = self::escape($root '.translation.' $field->getPropertyName());
  349.             //add selection for resolved parent-child and language inheritance
  350.             $query->addSelect(
  351.                 sprintf('COALESCE(%s)'implode(','$selects)) . ' as '
  352.                 self::escape($root '.' $field->getPropertyName())
  353.             );
  354.         }
  355.     }
  356.     public function joinVersion(QueryBuilder $queryEntityDefinition $definitionstring $rootContext $context): void
  357.     {
  358.         $table $definition->getEntityName();
  359.         $versionRoot $root '_version';
  360.         $query->andWhere(
  361.             str_replace(
  362.                 ['#root#''#table#''#version#'],
  363.                 [self::escape($root), self::escape($table), self::escape($versionRoot)],
  364.                 '#root#.version_id = COALESCE(
  365.                     (SELECT DISTINCT version_id FROM #table# AS #version# WHERE #version#.`id` = #root#.`id` AND `version_id` = :version),
  366.                     :liveVersion
  367.                 )'
  368.             )
  369.         );
  370.         $query->setParameter('liveVersion'Uuid::fromHexToBytes(Defaults::LIVE_VERSION));
  371.         $query->setParameter('version'Uuid::fromHexToBytes($context->getVersionId()));
  372.     }
  373.     public static function getTranslatedField(EntityDefinition $definitionTranslatedField $translatedField): Field
  374.     {
  375.         $translationDefinition $definition->getTranslationDefinition();
  376.         if ($translationDefinition === null) {
  377.             throw new \RuntimeException(sprintf('Entity %s has no translation definition'$definition->getEntityName()));
  378.         }
  379.         $field $translationDefinition->getFields()->get($translatedField->getPropertyName());
  380.         if ($field === null || !$field instanceof StorageAware || !$field instanceof Field) {
  381.             throw new \RuntimeException(
  382.                 sprintf(
  383.                     'Missing translated storage aware property %s in %s',
  384.                     $translatedField->getPropertyName(),
  385.                     $translationDefinition->getEntityName()
  386.                 )
  387.             );
  388.         }
  389.         return $field;
  390.     }
  391.     public static function buildTranslationChain(string $rootContext $contextbool $includeParent): array
  392.     {
  393.         $count = \count($context->getLanguageIdChain()) - 1;
  394.         for ($i $count$i >= 1; --$i) {
  395.             $chain[] = $root '.translation.fallback_' $i;
  396.             if ($includeParent) {
  397.                 $chain[] = $root '.parent.translation.fallback_' $i;
  398.             }
  399.         }
  400.         $chain[] = $root '.translation';
  401.         if ($includeParent) {
  402.             $chain[] = $root '.parent.translation';
  403.         }
  404.         return $chain;
  405.     }
  406.     public function addIdCondition(Criteria $criteriaEntityDefinition $definitionQueryBuilder $query): void
  407.     {
  408.         $primaryKeys $criteria->getIds();
  409.         $primaryKeys array_values($primaryKeys);
  410.         if (empty($primaryKeys)) {
  411.             return;
  412.         }
  413.         if (!\is_array($primaryKeys[0]) || \count($primaryKeys[0]) === 1) {
  414.             $primaryKeyField $definition->getPrimaryKeys()->first();
  415.             /** @feature-deprecated (flag:FEATURE_NEXT_14872) remove FeatureCheck
  416.              * if ($primaryKeyField instanceof IdField || $primaryKeyField instanceof FkField) {
  417.              */
  418.             if ($primaryKeyField instanceof IdField || (Feature::isActive('FEATURE_NEXT_14872') && $primaryKeyField instanceof FkField)) {
  419.                 $primaryKeys array_map(function ($id) {
  420.                     if (\is_array($id)) {
  421.                         /** @var string $shiftedId */
  422.                         $shiftedId array_shift($id);
  423.                         return Uuid::fromHexToBytes($shiftedId);
  424.                     }
  425.                     return Uuid::fromHexToBytes($id);
  426.                 }, $primaryKeys);
  427.             }
  428.             if (!$primaryKeyField instanceof StorageAware) {
  429.                 throw new \RuntimeException('Primary key fields has to be an instance of StorageAware');
  430.             }
  431.             $query->andWhere(sprintf(
  432.                 '%s.%s IN (:ids)',
  433.                 EntityDefinitionQueryHelper::escape($definition->getEntityName()),
  434.                 EntityDefinitionQueryHelper::escape($primaryKeyField->getStorageName())
  435.             ));
  436.             $query->setParameter('ids'$primaryKeysConnection::PARAM_STR_ARRAY);
  437.             return;
  438.         }
  439.         $this->addIdConditionWithOr($criteria$definition$query);
  440.     }
  441.     private function callResolver(FieldResolverContext $context): string
  442.     {
  443.         $resolver $context->getField()->getResolver();
  444.         if (!$resolver) {
  445.             return $context->getAlias();
  446.         }
  447.         return $resolver->join($context);
  448.     }
  449.     private function addIdConditionWithOr(Criteria $criteriaEntityDefinition $definitionQueryBuilder $query): void
  450.     {
  451.         $wheres = [];
  452.         foreach ($criteria->getIds() as $primaryKey) {
  453.             if (!\is_array($primaryKey)) {
  454.                 $primaryKey = ['id' => $primaryKey];
  455.             }
  456.             $where = [];
  457.             foreach ($primaryKey as $propertyName => $value) {
  458.                 $field $definition->getFields()->get($propertyName);
  459.                 /*
  460.                  * @deprecated tag:v6.5.0 - with 6.5.0 the only passing the propertyName will be supported
  461.                  */
  462.                 if (!$field) {
  463.                     $field $definition->getFields()->getByStorageName($propertyName);
  464.                 }
  465.                 if (!$field) {
  466.                     throw new UnmappedFieldException($propertyName$definition);
  467.                 }
  468.                 if (!$field instanceof StorageAware) {
  469.                     throw new \RuntimeException('Only storage aware fields are supported in read condition');
  470.                 }
  471.                 if ($field instanceof IdField || $field instanceof FkField) {
  472.                     $value Uuid::fromHexToBytes($value);
  473.                 }
  474.                 $key 'pk' Uuid::randomHex();
  475.                 $accessor EntityDefinitionQueryHelper::escape($definition->getEntityName()) . '.' EntityDefinitionQueryHelper::escape($field->getStorageName());
  476.                 /*
  477.                  * @deprecated tag:v6.5.0 - check for duplication in accessors will be removed,
  478.                  * when we only support propertyNames to be used in search and when IdSearchResult only returns the propertyNames
  479.                  */
  480.                 if (!\array_key_exists($accessor$where)) {
  481.                     $where[$accessor] = $accessor ' = :' $key;
  482.                     $query->setParameter($key$value);
  483.                 }
  484.             }
  485.             $wheres[] = '(' implode(' AND '$where) . ')';
  486.         }
  487.         $wheres implode(' OR '$wheres);
  488.         $query->andWhere($wheres);
  489.     }
  490.     private function getTranslationFieldAccessor(Field $fieldstring $accessor, array $chainContext $context): string
  491.     {
  492.         if (!$field instanceof StorageAware) {
  493.             throw new \RuntimeException('Only storage aware fields are supported as translated field');
  494.         }
  495.         $selects = [];
  496.         foreach ($chain as $part) {
  497.             $select $this->buildFieldSelector($part$field$context$accessor);
  498.             $selects[] = str_replace(
  499.                 '`.' self::escape($field->getStorageName()),
  500.                 '.' $field->getPropertyName() . '`',
  501.                 $select
  502.             );
  503.         }
  504.         /*
  505.          * Simplified Example:
  506.          * COALESCE(
  507.              JSON_UNQUOTE(JSON_EXTRACT(`tbl.translation.fallback_2`.`translated_attributes`, '$.path')) AS datetime(3), # child language
  508.              JSON_UNQUOTE(JSON_EXTRACT(`tbl.translation.fallback_1`.`translated_attributes`, '$.path')) AS datetime(3), # root language
  509.              JSON_UNQUOTE(JSON_EXTRACT(`tbl.translation`.`translated_attributes`, '$.path')) AS datetime(3) # system language
  510.            );
  511.          */
  512.         return sprintf('COALESCE(%s)'implode(','$selects));
  513.     }
  514.     private function buildInheritedAccessor(
  515.         Field $field,
  516.         string $root,
  517.         EntityDefinition $definition,
  518.         Context $context,
  519.         string $original
  520.     ): string {
  521.         if ($field instanceof TranslatedField) {
  522.             $inheritedChain self::buildTranslationChain($root$context$definition->isInheritanceAware() && $context->considerInheritance());
  523.             $translatedField self::getTranslatedField($definition$field);
  524.             return $this->getTranslationFieldAccessor($translatedField$original$inheritedChain$context);
  525.         }
  526.         $select $this->buildFieldSelector($root$field$context$original);
  527.         if (!$field->is(Inherited::class) || !$context->considerInheritance()) {
  528.             return $select;
  529.         }
  530.         $parentSelect $this->buildFieldSelector($root '.parent'$field$context$original);
  531.         return sprintf('IFNULL(%s, %s)'$select$parentSelect);
  532.     }
  533.     private function buildFieldSelector(string $rootField $fieldContext $contextstring $accessor): string
  534.     {
  535.         return $field->getAccessorBuilder()->buildAccessor($root$field$context$accessor);
  536.     }
  537. }