setConsiderInheritance example

static::assertSame((string) $dalProduct->getTranslation('description')$esProduct['description'][$ids->get('language-2')]);
            static::assertSame($dalProduct->getTranslation('customFields')$esProduct['customFields'][Defaults::LANGUAGE_SYSTEM]);
        } else {
            static::assertSame((string) $dalProduct->getTranslation('name')$esProduct['name']);
            static::assertSame((string) $dalProduct->getTranslation('description')$esProduct['description']);
            static::assertSame($dalProduct->getTranslation('customFields')$esProduct['customFields']);
        }

        // Fetch: Second language variant fallback to parent         $languageContext = new Context(new SystemSource()[], Defaults::CURRENCY, [$ids->get('language-2')$ids->get('language-1'), Defaults::LANGUAGE_SYSTEM]);
        $languageContext->addExtensions($context->getExtensions());
        $languageContext->setConsiderInheritance(true);

        $dal21 = $ids->getBytes('dal-2.1');

        $esProducts = $this->definition->fetch([$dal21]$languageContext);

        $esProduct = $esProducts[$ids->get('dal-2.1')];

        $criteria = new Criteria([$ids->get('dal-2.1')]);
        $dalProduct = $this->productRepository->search($criteria$languageContext)->first();

        static::assertInstanceOf(ProductEntity::class$dalProduct);
        
private readonly SeoUrlDataValidationFactoryInterface $seoUrlValidator,
        private readonly DataValidator $validator,
        private readonly EntityRepository $salesChannelRepository,
        private readonly RequestCriteriaBuilder $requestCriteriaBuilder,
        private readonly DefinitionInstanceRegistry $definitionInstanceRegistry
    ) {
    }

    #[Route(path: '/api/_action/seo-url-template/validate', name: 'api.seo-url-template.validate', methods: ['POST'])]     public function validate(Request $request, Context $context): JsonResponse
    {
        $context->setConsiderInheritance(true);

        $this->validateSeoUrlTemplate($request);
        $seoUrlTemplate = $request->request->all();

        // just call it to validate the template         $this->getPreview($seoUrlTemplate$context);

        return new JsonResponse();
    }

    #[Route(path: '/api/_action/seo-url-template/preview', name: 'api.seo-url-template.preview', methods: ['POST'])]
if ($id === null) {
            return null;
        }

        $criteria = new Criteria([$id]);

        return $this->loadProduct($criteria$storableFlow->getContext()$id);
    }

    private function loadProduct(Criteria $criteria, Context $context, string $id): ?ProductEntity
    {
        $context->setConsiderInheritance(true);

        $event = new BeforeLoadStorableFlowDataEvent(
            ProductDefinition::ENTITY_NAME,
            $criteria,
            $context,
        );

        $this->dispatcher->dispatch($event$event->getName());

        /** @var ProductEntity|null $product */
        $product = $this->productRepository->search($criteria$context)->get($id);

        
static::assertEquals(array_combine($expected$expected)$results->getIds());

        /** @var ArrayEntity $parent */
        $parent = $repo->search(new Criteria([$parentId])$context)->first();
        static::assertNotNull($parent);

        static::assertEquals('parent', $parent->get('name'));
        static::assertEquals(['foo' => 'bar']$parent->get('custom'));

        $criteria = new Criteria([$childId]);

        $context->setConsiderInheritance(true);
        $child = $repo->search($criteria$context)->first();
        static::assertNotNull($child);

        static::assertEquals('child', $child->get('name'));
        static::assertEquals(['foo' => 'bar']$child->get('custom'));

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('custom.foo', 'bar'));

        $results = $repo->search($criteria$context);
        $expected = [$parentId$childId];
        
return $existingProducts;
    }

    /** * @param array<string> $ids * @param array<int, ConfigField> $configFields * * @return RepositoryIterator<ProductCollection> */
    private function getIterator(array $ids, Context $context, array $configFields): RepositoryIterator
    {
        $context->setConsiderInheritance(true);

        $criteria = new Criteria($ids);
        $criteria->setLimit(50);

        $this->buildCriteria(array_column($configFields, 'field')$criteria$context);

        return new RepositoryIterator($this->productRepository, $context$criteria);
    }

    /** * @param array<string> $ids */


    public function testMaxJoinBug(): void
    {
        $context = new Context(
            new SystemSource(),
            [],
            Defaults::CURRENCY,
            [Uuid::randomHex(), Uuid::randomHex(), Defaults::LANGUAGE_SYSTEM]
        );

        $context->setConsiderInheritance(true);

        // creates a select with 20x base tables         // original each table gets 3x translation tables as join table         // this results in a query of 79x joins         $criteria = new Criteria();
        $criteria->addAssociation('type');
        $criteria->addAssociation('language.locale');
        $criteria->addAssociation('language.translationCode');
        $criteria->addAssociation('customerGroup');
        $criteria->addAssociation('currency');
        $criteria->addAssociation('paymentMethod.media');
        
'parentId' => $parentId,
                'name' => 'green',
                'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => 100, 'net' => 100, 'linked' => true]],
                'tax' => ['id' => $greenTax, 'taxRate' => 13, 'name' => 'green tax'],
            ],
        ];

        $this->productRepository->create($products, Context::createDefaultContext());

        $criteria = new Criteria([$parentId$greenId$redId]);
        $context = Context::createDefaultContext();
        $context->setConsiderInheritance(false);
        $products = $this->productRepository->search($criteria$context);

        /** @var ProductEntity $parent */
        $parent = $products->get($parentId);
        static::assertInstanceOf(ProductEntity::class$parent);
        static::assertInstanceOf(TaxEntity::class$parent->getTax());
        static::assertInstanceOf(Price::class$parent->getCurrencyPrice(Defaults::CURRENCY));
        static::assertEquals(50, $parent->getCurrencyPrice(Defaults::CURRENCY)->getGross());

        /** @var ProductEntity $red */
        $red = $products->get($redId);

        
$productData['children'] = [[
            'id' => $variantId,
            'productNumber' => Uuid::randomHex(),
            'stock' => 1,
            'options' => [
                [
                    'id' => $optionId,
                ],
            ],
        ]];

        $this->salesChannelContext->getContext()->setConsiderInheritance(true);
        $this->productRepository->create([$productData]$this->salesChannelContext->getContext());

        $result = $this->route->load($variantIdnew Request()$this->salesChannelContext, new Criteria())->getResult();

        static::assertEquals(2, $result->count());
        foreach ($result as $index => $element) {
            static::assertEquals($crossSellingIds[$index]$element->getCrossSelling()->getId());
        }
    }

    private function createProductStream(bool $includesIsCloseoutProducts = false, bool $noStock = false): string
    {
// name should be inherited             [
                'id' => $greenId,
                'productNumber' => Uuid::randomHex(),
                'stock' => 10,
                'price' => [$greenPrice],
                'parentId' => $parentId,
            ],
        ];

        $context = Context::createDefaultContext();
        $context->setConsiderInheritance(true);
        $this->repository->create($products$context);

        $criteria = new Criteria([$redId$greenId]);
        /** @var ProductCollection $products */
        $products = $this->repository->search($criteria$context)->getEntities();

        $criteria = new Criteria([$parentId]);
        /** @var ProductCollection $parents */
        $parents = $this->repository->search($criteria$context);

        static::assertTrue($parents->has($parentId));
        
static::assertArrayNotHasKey('child', $first->getTranslated()['customTranslated']);
        static::assertSame(1, $first->getTranslated()['customTranslated']['int']);

        $translated = $first->getTranslated();
        static::assertSame(1.0, $translated['customTranslated']['systemFloat']);
        static::assertArrayNotHasKey('root', $translated['customTranslated']);
        static::assertArrayNotHasKey('child', $translated['customTranslated']);
        static::assertSame(1, $translated['customTranslated']['int']);

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('customTranslated.root', true));
        $context->setConsiderInheritance(true);
        $result = $repo->search($criteria$context);
        $expected = [];
        static::assertEquals(array_combine($expected$expected)$result->getIds());

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('customTranslated.child', $now));
        $result = $repo->search($criteria$context);
        $expected = [];
        static::assertEquals(array_combine($expected$expected)$result->getIds());

        // root -> system
$filter = json_decode((string) $filter, true, 512, \JSON_THROW_ON_ERROR);

        $criteria = $this->getCriteria($filter);

        if ($criteria === null) {
            return;
        }

        $criteria->setLimit(150);
        $considerInheritance = $message->getContext()->considerInheritance();
        $message->getContext()->setConsiderInheritance(true);

        $iterator = new RepositoryIterator(
            $this->repository,
            $message->getContext(),
            $criteria
        );

        $binary = Uuid::fromHexToBytes($id);

        $ids = $this->connection->fetchFirstColumn(
            'SELECT LOWER(HEX(product_id)) FROM product_stream_mapping WHERE product_stream_id = :id',
            [
$inheritance = $connection->fetchAssociative(
            'SELECT LOWER(HEX(product_manufacturer_id)) as fk, LOWER(HEX(manufacturer)) as association FROM product WHERE id = :id',
            ['id' => Uuid::fromHexToBytes($id)]
        );

        static::assertIsArray($inheritance);
        static::assertEquals($fk$inheritance['fk']);
        static::assertEquals($association$inheritance['association']);

        $context = Context::createDefaultContext();
        $context->setConsiderInheritance(true);

        $criteria = new Criteria([$id]);
        $criteria->addAssociation('manufacturer');

        /** @var ProductEntity $product */
        $product = $this->getContainer()->get('product.repository')
            ->search($criteria$context)
            ->get($id);

        static::assertEquals($association$product->getManufacturerId());
        static::assertEquals($association$product->getManufacturer()->getId());
    }


    private function assertProductNames(array $assertions, string $id): void
    {
        foreach ($assertions as [$name$context]) {
            $this->assertProductName($name$id$context);
        }
    }

    private function assertProductName(string $name, string $id, Context $context): void
    {
        $context->setConsiderInheritance(true);

        /** @var ProductEntity $product */
        $product = $this->getContainer()
            ->get('product.repository')
            ->search(new Criteria([$id])$context)->first();

        static::assertTrue($context->considerInheritance());
        static::assertSame($name$product->getTranslated()['name']sprintf(
            'Expected %s with language chain %s but got %s, version context: %s',
            $name,
            (string) print_r($context->getLanguageIdChain(), true),
            
$event = $container->get('product.repository')
            ->upsert([$product], Context::createDefaultContext());

        static::assertNotEmpty($event->getPrimaryKeys('product'));
        static::assertNotEmpty($event->getPrimaryKeys('custom_entity_blog'));
        static::assertNotEmpty($event->getPrimaryKeys('custom_entity_blog_inherited_products'));

        static::assertContains($ids->get('inh.blog.1')$event->getPrimaryKeys('custom_entity_blog'));
        static::assertContains($ids->get('inh.blog.2')$event->getPrimaryKeys('custom_entity_blog'));

        $context = Context::createDefaultContext();
        $context->setConsiderInheritance(true);

        $criteria = new Criteria($ids->getList(['v1', 'v2']));
        $criteria->addAssociation('customEntityBlogInheritedProducts');

        $products = $container->get('product.repository')->search($criteria$context);

        static::assertCount(2, $products);
        $v1 = $products->get($ids->get('v1'));

        static::assertInstanceOf(ProductEntity::class$v1);
        static::assertTrue($v1->hasExtension('customEntityBlogInheritedProducts'));
        
$salesChannels = $this->salesChannelRepository->search($criteria$context)->getEntities();

        foreach ($templates as $config) {
            $template = $config['template'];
            $salesChannel = $salesChannels->get($config['salesChannelId']);
            if ($template === '' || $salesChannel === null) {
                continue;
            }

            $chain = $languageChains[$config['languageId']];
            $languageContext = new Context(new SystemSource()[], Defaults::CURRENCY, $chain);
            $languageContext->setConsiderInheritance(true);

            // generate new seo urls             $urls = $this->seoUrlGenerator->generate($ids$template$route$languageContext$salesChannel);

            // persist seo urls to storage             $this->seoUrlPersister->updateSeoUrls($languageContext$routeName$ids$urls$salesChannel);
        }
    }

    /** * Loads the SEO url templates for the given $routeName for all combinations of languages and sales channels * * @param non-empty-string $routeName * * @return list<array{salesChannelId: string, languageId: string, template: string}> */
Home | Imprint | This part of the site doesn't use cookies.