2

akeneo.storage.pre_saveAkeneo 2.2.8 を使用しており、 -event を使用して元の製品データと提供された新しいデータを比較しようとしています。akeneo.storage.pre_save-eventにサブスクライブすることでこれを行います。

event_subscribers.yml

parameters:
    vendor.bundle.event_subscriber.product_save.class: Vendor\Bundle\CustomBundle\EventSubscriber\ProductSaveSubscriber

services:
    vendor.bundle.event_subscriber.product_save:
        class: '%vendor.bundle.event_subscriber.product_save.class%'
        arguments:
            - '@pim_catalog.repository.product'
        tags:
            - { name: kernel.event_listener, event: akeneo.storage.pre_save, method: onPreSave, priority: 255 }

ProductSaveSubscriber.php

/**
 * @var ProductRepositoryInterface
 */
protected $productRepository;

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

public function onPreSave(GenericEvent $event)
{
    /** @var Product $subject */
    $subject = $event->getSubject();

    if ($subject instanceof Product) {

        $originalProduct = $this->productRepository->findOneByIdentifier($subject->getIdentifier());

        foreach ($subject->getAttributes() as $attribute) {
            if ($attribute->getReadOnly()) {
                echo "{$attribute->getCode()} : {$subject->getValue($attribute->getCode())}\n";
                echo "{$attribute->getCode()} : {$originalProduct->getValue($attribute->getCode())}\n";
            }
        }
    }
}

このコードを実行すると、2 番目のecho-statement で元のデータが返されることが期待されます (これを新たにロードしたため)。ただし、リポジトリからロードした元の製品にも新しいデータがあります。

ここで注意すべきもう 1 つの点は、die()-statement を追加すると、データがデータベースに保存されないことです。そのため、リポジトリはメモリ内モデルなどを返すようです。

誰かが私を正しい方向に向けることができますか? または、新しく入力したデータを既存のデータと比較するために間違ったアプローチを使用していますか?

4

1 に答える 1