我正在使用Akeneo 2.2.8,我正在尝试使用
akeneo.storage.pre_save
-事件将原始产品数据与提供的新数据进行比较。我通过订阅
akeneo.storage.pre_保存
-事件:
在
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";
}
}
}
}
现在,当我运行此代码时,我希望第二个
echo
-提供原始数据的语句(因为我重新加载了它)。但是,我从存储库加载的原始产品也有新的数据。
这里要注意的另一件事是,如果我添加
die()
-语句,数据不存储在数据库中。因此,似乎存储库返回内存模型或类似的模型。
有人能给我指出正确的方向吗?或者我是否使用了错误的方法来比较新输入的数据和已经存在的数据?