如何在 Shopware 6 导入期间替换(不添加)属性?

How to replace (not add) properties during Import in Shopware 6?

我们正在使用 Shopware 6.3.5 并希望在产品导入(更新)期间替换属性。

我们指定产品 ID 和列表或 属性 UUID(在 properties 列中)

目前,这些被添加到数据库中的现有属性中。

这似乎是由于导入期间 upsert 调用的行为所致。

如何改变?

我们试过这个:

DI:

<argument type="service" id="product.repository"/>

方法:

class ImportSubscriber implements EventSubscriberInterface
{
    private EntityRepositoryInterface $productRepository;

    public function __construct(EntityRepositoryInterface $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            ImportExportBeforeImportRecordEvent::class => 'onImportExportBeforeImportRecord'
        ];
    }

    public function onImportExportBeforeImportRecord(ImportExportBeforeImportRecordEvent $event)
    {
        $this->productRepository->update([
            [
                'id' => $event->getRecord()['id'],
                'property_ids' => null,
            ]
        ], $event->getContext());
    }
}

但是这个更新语句导致 \Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteTypeIntendException in \Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommandQueue::ensureIs

我也想知道这个 WriteCommandQueue 是做什么的,如果它的级别太低,我想做什么?

此外,我想知道 property_ids 是否是要更改的正确字段,或者实际上我必须清除该导入行的 product_property table?

编辑

接下来我试了这个

DI:

<argument type="service" id="product_property.repository"/>

方法:

    public function onImportExportBeforeImportRecord(ImportExportBeforeImportRecordEvent $event)
    {
        $existing = $this->productPropertryRepository->search(
            (new Criteria())->addFilter(new EqualsFilter('productId', $event->getRecord()['id'])),
            $event->getContext()
        );
        $entities = $existing->getEntities();
        foreach($entities as $entity) {
            $this->productPropertryRepository->delete([
                [
                    'productId' => $event->getRecord()['id'],
                    'optionId' => $entity->get('optionId')
                ]
            ], $event->getContext());
        }
    }

但是我得到一个

Shopware\Core\Framework\DataAbstractionLayer\Exception\MappingEntityClassesException

Mapping definition neither have entities nor collection.

搜索方法不适用于映射定义。您可以使用 searchIds 获取 ID 并像您一样调用删除。

根据@Shyim 的回答,这个有效:

    public function onImportExportBeforeImportRecord(ImportExportBeforeImportRecordEvent $event)
    {
        $existing = $this->productPropertyRepository->searchIds(
            (new Criteria())->addFilter(new EqualsFilter('productId', $event->getRecord()['id'])),
            $event->getContext()
        );

        foreach($existing->getIds() as $id) {
            if (in_array($id['property_group_option_id'], ($event->getRecord()['properties'] ?? [] ))) {
                continue;
            }

            $this->productPropertyRepository->delete([
                [
                    'productId' => $event->getRecord()['id'],
                    'optionId' => $id['property_group_option_id']
                ]
            ], $event->getContext());
        }
    }

注意:这将删除所有当前属性,更复杂的解决方案可能是仅针对特定属性(例如,通过在导入中提供名称列表)或具有一个 positive/negative 列表,其中的属性应该是清除或保留。