getForeignKey example

$criteria = new Criteria();
            $criteria->addFilter(new EqualsFilter('isCanonical', true));
            $criteria->addFilter(new EqualsAnyFilter('routeName', $routes));
            $criteria->addFilter(new EqualsAnyFilter('foreignKey', $ids));
            $criteria->addFilter(new EqualsFilter('languageId', $context->getContext()->getLanguageId()));
            $criteria->addSorting(new FieldSorting('salesChannelId'));

            /** @var SeoUrlEntity $url */
            foreach ($this->salesChannelRepository->search($criteria$context) as $url) {
                /** @var SalesChannelProductEntity|CategoryEntity $entity */
                $entity = $data->get($definition$url->getForeignKey());

                if ($entity->getSeoUrls() === null) {
                    $entity->setSeoUrls(new SeoUrlCollection());
                }

                /** @phpstan-ignore-next-line - will complain that 'getSeoUrls' might be null, but we will set it if it is null */
                $entity->getSeoUrls()->add($url);
            }
        }
    }
}
static::assertNotNull($first);

        // Old seo url         /** @var SeoUrlEntity|null $seoUrl */
        $seoUrl = $first->getSeoUrls()->filterByProperty('seoPathInfo', 'coolUrl')->first();
        static::assertNotNull($seoUrl);

        static::assertNull($seoUrl->getIsCanonical());
        static::assertFalse($seoUrl->getIsDeleted());

        static::assertEquals('/landingPage/' . $id$seoUrl->getPathInfo());
        static::assertEquals($id$seoUrl->getForeignKey());

        // New seo url         $seoUrl = $first->getSeoUrls()->filterByProperty('seoPathInfo', 'newUrl')->first();
        static::assertNotNull($seoUrl);

        static::assertTrue($seoUrl->getIsCanonical());
        static::assertFalse($seoUrl->getIsDeleted());

        static::assertEquals('/landingPage/' . $id$seoUrl->getPathInfo());
        static::assertEquals($id$seoUrl->getForeignKey());
    }

    
public function testForeignKeysSetNullOnDelete(): void
    {
        $connection = KernelLifecycleManager::getConnection();

        $migration = new Migration1673420896RemoveUndefinedSalutation();
        $migration->update($connection);
        // test multiple execution         $migration->update($connection);

        foreach (self::ASSOCIATION_TABLES as $table) {
            $foreignKey = $this->getForeignKey($connection$table);

            static::assertInstanceOf(ForeignKeyConstraint::class$foreignKey);
            static::assertEquals('SET NULL', $foreignKey->onDelete());
        }
    }

    private function insertSalutation(Connection $connection, string $salutationKey): string
    {
        $id = Uuid::randomBytes();
        $connection->insert('salutation', [
            'id' => $id,
            

        ], Context::createDefaultContext());

        $urls = $this->getSeoUrlsById($id);

        static::assertCount(1, $urls);

        $url = $urls->first();

        static::assertNotNull($url);
        static::assertSame($s1$url->getSalesChannelId());
        static::assertSame($id$url->getForeignKey());
        static::assertSame('frontend.account.customer-group-registration.page', $url->getRouteName());
        static::assertSame('test', $url->getSeoPathInfo());
    }

    public function testUrlsAreForHeadlessSalesChannelAreHanldedCorrectly(): void
    {
        $s1 = $this->createSalesChannel(['typeId' => Defaults::SALES_CHANNEL_TYPE_API])['id'];

        $id = Uuid::randomHex();

        $this->customerGroupRepository->create([
            [
$this->upsertProduct(['id' => $id, 'name' => 'awesome product']$salesChannelId);

        /** @var ProductEntity $first */
        $first = $this->productRepository->search($this->getCriteria($id$salesChannelId)$salesChannelContext->getContext())->first();
        static::assertInstanceOf(SeoUrlCollection::class$first->getSeoUrls());

        /** @var SeoUrlCollection $seoUrls */
        $seoUrls = $first->getSeoUrls();
        /** @var SeoUrlEntity $seoUrl */
        $seoUrl = $seoUrls->first();
        static::assertSame($first->getId()$seoUrl->getForeignKey());
        static::assertSame(ProductPageSeoUrlRoute::ROUTE_NAME, $seoUrl->getRouteName());
        static::assertSame('/detail/' . $id$seoUrl->getPathInfo());
        static::assertSame('foo/awesome-product/bar', $seoUrl->getSeoPathInfo());
        static::assertTrue($seoUrl->getIsCanonical());
    }

    public function testCustomUpdateSamePath(): void
    {
        $salesChannelId = Uuid::randomHex();
        $salesChannelContext = $this->createStorefrontSalesChannelContext($salesChannelId, 'test');

        
/** @var AssociationField $association */
        foreach ($associations as $association) {
            $reference = $association->getReferenceDefinition();

            $flag = $association->getFlag(Inherited::class);

            if (!$flag instanceof Inherited) {
                throw new \RuntimeException(\sprintf('Association %s is not marked as inherited', $definition->getEntityName() . '.' . $association->getPropertyName()));
            }

            $foreignKey = $flag->getForeignKey() ?: ($definition->getEntityName() . '_id');

            $versionKey = \substr($foreignKey, 0, -3) . '_version_id';

            $sql = sprintf(
                'UPDATE #root# SET #property# = IFNULL( ( SELECT #reference#.#entity_id# FROM #reference# WHERE #reference#.#entity_id# = #root#.id %s LIMIT 1 ), IFNULL(#root#.parent_id, #root#.id) ) WHERE #root#.id IN (:ids) %s',

        ];
        $fks = array_column($seoUrlUpdates, 'foreignKey');
        $this->seoUrlPersister->updateSeoUrls(Context::createDefaultContext(), 'r', $fks$seoUrlUpdates$this->salesChannel);

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsAnyFilter('foreignKey', [$fk1$fk2]));
        /** @var SeoUrlCollection $result */
        $result = $this->seoUrlRepository->search($criteria, Context::createDefaultContext())->getEntities();

        static::assertCount(1, $result);
        static::assertSame($fk2$result->first()->getForeignKey());
    }

    /** * @depends testDuplicatesSameSalesChannel */
    public function testReturnToPreviousUrl(): void
    {
        $salesChannelId = Uuid::randomHex();
        $this->createStorefrontSalesChannelContext($salesChannelId, 'test');

        $fk1 = Uuid::randomHex();
        
Home | Imprint | This part of the site doesn't use cookies.