UpdateCommand example


    private function updateCommandQueue(
        EntityDefinition $definition,
        WriteParameterBag $parameterBag,
        EntityExistence $existence,
        array $pkData,
        array $data
    ): void {
        $queue = $parameterBag->getCommandQueue();

        if ($existence->exists()) {
            $queue->add($definitionnew UpdateCommand($definition$data$pkData$existence$parameterBag->getPath()));

            return;
        }

        $queue->add($definitionnew InsertCommand($definitionarray_merge($pkData$data)$pkData$existence$parameterBag->getPath()));
    }

    /** * @return Field[] */
    private function getFieldsInWriteOrder(EntityDefinition $definition): array
    {

        $fieldCollection = $this->definition->getFields();

        $oneToManyField = $fieldCollection->get('oneToMany');
        $manyToOneField = $fieldCollection->get('manyToOne');

        static::assertInstanceOf(OneToManyAssociationField::class$oneToManyField);
        static::assertInstanceOf(ManyToOneAssociationField::class$manyToOneField);

        $event = new PreWriteValidationEvent(WriteContext::createFromContext(Context::createDefaultContext())[
            new DeleteCommand($oneToManyField->getReferenceDefinition()[]$this->createMock(EntityExistence::class)),
            new UpdateCommand($manyToOneField->getReferenceDefinition()[][]$this->createMock(EntityExistence::class), ''),
            new UpdateCommand($oneToManyField->getReferenceDefinition()['rule_id' => 'foo'][]$this->createMock(EntityExistence::class), ''),
        ]);

        $this->areaUpdater->triggerChangeSet($event);

        /** @var DeleteCommand[]|UpdateCommand[] $commands */
        $commands = $event->getCommands();

        static::assertCount(3, $commands);
        static::assertTrue($commands[0]->requiresChangeSet());
        static::assertFalse($commands[1]->requiresChangeSet());
        
$stockSubscriber = new OrderStockSubscriber(
            $this->createMock(Connection::class),
            $stockStorage,
            true
        );

        $definition = $this->getDefinition();

        $event = EntityWriteEvent::create(
            WriteContext::createFromContext($context),
            [
                new UpdateCommand(
                    $definition,
                    ['some-field' => 'some-value'],
                    ['id' => $this->ids->getBytes('item-1')],
                    new EntityExistence(
                        OrderLineItemDefinition::ENTITY_NAME,
                        ['id' => $this->ids->get('item-1')],
                        true,
                        false,
                        false,
                        []
                    ),
                    
$subscriber = new ScheduledTaskCompatibilitySubscriber();
        $subscriber->addBackwardsCompatibility($event);

        static::assertEquals([$insertCommand]$event->getCommands());
    }

    public static function addBackwardsCompatibilityProvider(): \Generator
    {
        $dummyExistence = new EntityExistence('', [], true, true, true, []);

        $updateCommand = new UpdateCommand(new ScheduledTaskDefinition()[
            'id' => 'id',
            'name' => 'name',
            'run_interval' => 1,
        ][]$dummyExistence, '');

        yield 'skip if not insert command' => [
            $updateCommand,
            $updateCommand,
        ];

        $insertCommand = new InsertCommand(new ProductDefinition()[
            
static::assertArrayHasKey(OrderEvents::ORDER_LINE_ITEM_WRITTEN_EVENT, $events);
        static::assertArrayHasKey(OrderEvents::ORDER_LINE_ITEM_DELETED_EVENT, $events);
    }

    public function testTriggerChangeSetWithNonLiveVersion(): void
    {
        $definition = new OrderLineItemDefinition();
        $primaryKey = ['id' => 'some_id'];
        $existence = new EntityExistence('order_line_item', [], false, false, false, []);
        $path = 'order_line_items';

        $commands = [new UpdateCommand(
            $definition,
            ['referenced_id' => 'new_referenced_id'],
            $primaryKey,
            $existence,
            $path
        )];

        $stockSubscriber = new StockUpdater(
            $this->getConnectionMock(),
            $this->dispatcher,
            new StockUpdateFilterProvider([]),
        );
$affectedDefinition = $this->registry->getByEntityName($affectedDefinitionClass);

            foreach ($keys as $key) {
                if (!\is_array($key)) {
                    $key = ['id' => $key];
                }

                $primary = EntityHydrator::encodePrimaryKey($affectedDefinition$key$writeContext->getContext());

                $existence = new EntityExistence($affectedDefinition->getEntityName()$primary, true, false, false, []);

                $queue->add($affectedDefinitionnew UpdateCommand($affectedDefinition[]$primary$existence, ''));
            }
        }
    }

    /** * @param array<mixed> $resolved */
    private function addDeleteCascadeCommands(WriteCommandQueue $queue, EntityDefinition $definition, WriteContext $writeContext, array $resolved): void
    {
        if ($definition instanceof MappingEntityDefinition) {
            return;
        }
/** * Gets the default commands that should always be available. * * @return array An array of default Command instances */
    protected function getDefaultCommands()
    {
        // Keep the core default commands to have the HelpCommand         // which is used when using the --help option         $defaultCommands = parent::getDefaultCommands();

        $defaultCommands[] = new UpdateCommand();

        return $defaultCommands;
    }

    private function registerErrorHandler()
    {
        set_error_handler(function D$errno$errstr$errfile$errline) {
            // error was suppressed with the @-operator             if (error_reporting() === 0 || $errno === E_USER_DEPRECATED) {
                return false;
            }

            
return $definition;
    }

    public function testBeforeWriteOnlyReactsToLiveVersions(): void
    {
        $context = Context::createDefaultContext()->createWithVersionId($this->ids->create('version'));

        $subscriber = new AvailableStockMirrorSubscriber();

        $definition = $this->getDefinition();

        $command = new UpdateCommand(
            $definition,
            ['stock' => 10],
            ['id' => $this->ids->getBytes('product-1')],
            new EntityExistence(
                ProductDefinition::ENTITY_NAME,
                ['id' => $this->ids->get('product-1')],
                true,
                false,
                false,
                []
            ),
            
Home | Imprint | This part of the site doesn't use cookies.