getWriteResults example

return [
            'customer_group_translation.written' => 'updatedCustomerGroup',
            'customer_group_registration_sales_channels.written' => 'newSalesChannelAddedToCustomerGroup',
            'customer_group_translation.deleted' => 'deleteCustomerGroup',
        ];
    }

    public function newSalesChannelAddedToCustomerGroup(EntityWrittenEvent $event): void
    {
        $ids = [];

        foreach ($event->getWriteResults() as $writeResult) {
            /** @var array<string, string> $pk */
            $pk = $writeResult->getPrimaryKey();
            $ids[] = $pk['customerGroupId'];
        }

        if (\count($ids) === 0) {
            return;
        }

        $this->createUrls($ids$event->getContext());
    }

    
public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
    {
        $categoryEvent = $event->getEventByEntityName(CategoryDefinition::ENTITY_NAME);

        if (!$categoryEvent) {
            return null;
        }

        $ids = $categoryEvent->getIds();
        $idsWithChangedParentIds = [];
        foreach ($categoryEvent->getWriteResults() as $result) {
            if (!$result->getExistence()) {
                continue;
            }
            $state = $result->getExistence()->getState();

            if (isset($state['parent_id'])) {
                $ids[] = Uuid::fromBytesToHex($state['parent_id']);
            }

            $payload = $result->getPayload();
            if (\array_key_exists('parentId', $payload)) {
                


        if (!$result || !$result->getEvents()) {
            return $overallResults;
        }

        foreach ($result->getEvents() as $event) {
            if (!$event instanceof EntityWrittenEvent) {
                continue;
            }

            foreach ($event->getWriteResults() as $writeResult) {
                $entityResult = $overallResults[$writeResult->getEntityName()] ?? $defaultTemplate;
                ++$entityResult[$writeResult->getOperation()];
                $overallResults[$writeResult->getEntityName()] = $entityResult;
            }
        }

        return $overallResults;
    }
}
if ($command->getDefinition()->getEntityName() !== ProductReviewDefinition::ENTITY_NAME) {
                continue;
            }

            $command->requestChangeSet();
        }
    }

    public function onReviewDeleted(EntityDeletedEvent $event): void
    {
        foreach ($event->getWriteResults() as $result) {
            if ($result->getEntityName() !== ProductReviewDefinition::ENTITY_NAME) {
                continue;
            }

            $changeset = $result->getChangeSet();
            \assert($changeset instanceof ChangeSet);

            $id = $changeset->getBefore('customer_id');

            if (!\is_string($id)) {
                continue;
            }
static::assertCount(2, $cascade);

        $cascade = $event->getPrimaryKeys('custom_entity_blog_translation');
        static::assertCount(1, $cascade);

        $categories = $event->getEventByEntityName('category');

        static::assertInstanceOf(EntityWrittenEvent::class$categories);
        static::assertContains($ids->get('blog-4-c-set-null-1')$categories->getIds());
        static::assertContains($ids->get('blog-4-c-set-null-2')$categories->getIds());

        foreach ($categories->getWriteResults() as $result) {
            static::assertArrayHasKey('customEntityBlogLinksSetNullId', $result->getPayload());
            static::assertNull($result->getProperty('customEntityBlogLinksSetNullId'));
        }

        $blog = self::blog('blog-5', $ids);
        $blog['topSellerRestrict'] = (new ProductBuilder($ids, 'top-seller-restrict'))->price(100)->build();
        $blog['linksRestrict'] = [['id' => $ids->get('category-restrict-delete'), 'name' => 'test', 'type' => self::CATEGORY_TYPE]];

        $blogRepository->create([$blog], Context::createDefaultContext());

        try {
            

    }

    /** * @param FkField[] $fields * @param string[] $ruleIds * * @return string[] */
    private function hydrateRuleIds(array $fields, EntityWrittenEvent $nestedEvent, array $ruleIds): array
    {
        foreach ($nestedEvent->getWriteResults() as $result) {
            $changeSet = $result->getChangeSet();
            $payload = $result->getPayload();

            foreach ($fields as $field) {
                if ($changeSet && $changeSet->hasChanged($field->getStorageName())) {
                    $ruleIds[] = $changeSet->getBefore($field->getStorageName());
                    $ruleIds[] = $changeSet->getAfter($field->getStorageName());
                }

                if ($changeSet) {
                    continue;
                }

    public function getIds(string $entity): WrittenEventIdCollection
    {
        $writtenEvent = $this->containerEvent->getEventByEntityName($entity);

        return new WrittenEventIdCollection(
            $writtenEvent ? $writtenEvent->getWriteResults() : []
        );
    }
}
if ($event === null) {
            return;
        }

        if (!$this->elasticsearchHelper->allowIndexing()) {
            return;
        }

        $newCreatedFields = [];

        foreach ($event->getWriteResults() as $writeResult) {
            $existence = $writeResult->getExistence();

            if ($existence && $existence->exists()) {
                continue;
            }

            /** @var array<mixed> $esType */
            $esType = self::getTypeFromCustomFieldType($writeResult->getProperty('type'));

            $newCreatedFields[(string) $writeResult->getProperty('name')] = $esType;
        }

        
foreach ($this->events as $event) {
            if ($event->getEntityName() !== $entity) {
                continue;
            }

            if (!$closure) {
                $ids = array_merge($ids$event->getIds());

                continue;
            }

            foreach ($event->getWriteResults() as $result) {
                if ($closure($result)) {
                    $ids[] = $result->getPrimaryKey();
                }
            }
        }

        return $ids;
    }
}

        );

        $context = Context::createDefaultContext();

        $container = $this->productRepository->create([$product]$context);

        $event = $container->getEventByEntityName(ProductVisibilityDefinition::ENTITY_NAME);

        // visibility created?         static::assertInstanceOf(EntityWrittenEvent::class$event);
        static::assertCount(2, $event->getWriteResults());

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

        /** @var ProductEntity $product */
        $product = $this->productRepository->search($criteria$context)->first();

        // check visibilities can be loaded as association         static::assertInstanceOf(ProductEntity::class$product);
        static::assertInstanceOf(ProductVisibilityCollection::class$product->getVisibilities());
        static::assertCount(2, $product->getVisibilities());

        
$promotionRepository = $this->getContainer()->get('promotion.repository');

        /** @var EntityRepository $promotionIndividualRepository */
        $promotionIndividualRepository = $this->getContainer()->get('promotion_individual_code.repository');

        $voucherA = $this->ids->create('voucherA');

        $writtenEvent = $this->createPromotion($voucherA$voucherA$promotionRepository$salesChannelContext);
        $promotionEvent = $writtenEvent->getEventByEntityName(PromotionDefinition::ENTITY_NAME);

        static::assertNotNull($promotionEvent);
        static::assertNotEmpty($promotionEvent->getWriteResults()[0]);
        $promotionId = $promotionEvent->getWriteResults()[0]->getPayload()['id'];

        $userId = Uuid::randomHex();
        $origin = new AdminApiSource($userId);
        $origin->setIsAdmin(true);
        $context = Context::createDefaultContext($origin);

        $event = $this->createIndividualCode($promotionId, 'CODE-1', $promotionIndividualRepository$context);

        $result = $indexer->update($event);

        


    public static function getSubscribedEvents(): array
    {
        return [
            'product_export.written' => 'afterWrite',
        ];
    }

    public function afterWrite(EntityWrittenEvent $event): void
    {
        foreach ($event->getWriteResults() as $writeResult) {
            if (!$this->productExportWritten($writeResult)) {
                continue;
            }

            $primaryKey = $writeResult->getPrimaryKey();
            $primaryKey = \is_array($primaryKey) ? $primaryKey['id'] : $primaryKey;

            $this->productExportRepository->update(
                [
                    [
                        'id' => $primaryKey,
                        

        return $permissions->isAllowed($this->event->getEntityName(), AclRoleDefinition::PRIVILEGE_READ);
    }

    /** * @return array{entity: string, operation: string, primaryKey: array<string, string>|string, updatedFields?: array<string>}[] */
    public function getPayloadFromEvent(EntityWrittenEvent $event): array
    {
        $payload = [];

        foreach ($event->getWriteResults() as $writeResult) {
            $result = [
                'entity' => $writeResult->getEntityName(),
                'operation' => $writeResult->getOperation(),
                'primaryKey' => $writeResult->getPrimaryKey(),
            ];

            if (!$event instanceof EntityDeletedEvent) {
                $result['updatedFields'] = array_keys($writeResult->getPayload());
            }

            $payload[] = $result;
        }


    public function mergeWriteResults(
        EntityWrittenEvent $writtenEvent,
        ?EntityWrittenEvent $translationEvent
    ): ?EntityWrittenEvent {
        if ($writtenEvent instanceof EntityDeletedEvent) {
            return $writtenEvent;
        }

        $mergedWriteResults = [];
        foreach ($writtenEvent->getWriteResults() as $writeResult) {
            if ($translationEvent) {
                $mergedWriteResults[] = $this->getMergedWriteResult($translationEvent$writeResult);

                continue;
            }

            if (empty($writeResult->getPayload())) {
                continue;
            }

            $mergedWriteResults[] = $writeResult;
        }
return;
        }

        $variants = $this->parseVariantString($row['variants']);

        $entityWrittenEvent = $entityWrittenEvents->filter(fn ($event) => $event instanceof EntityWrittenEvent && $event->getEntityName() === ProductDefinition::ENTITY_NAME)->first();

        if (!$entityWrittenEvent instanceof EntityWrittenEvent) {
            return;
        }

        $writeResults = $entityWrittenEvent->getWriteResults();

        if (empty($writeResults)) {
            return;
        }

        $parentId = $writeResults[0]->getPrimaryKey();
        $parentPayload = $writeResults[0]->getPayload();

        if (!\is_string($parentId)) {
            return;
        }

        
Home | Imprint | This part of the site doesn't use cookies.