代码之家  ›  专栏  ›  技术社区  ›  Giel Berkers

akeneo 2.2.8:如何在akeneo.storage.pre_save事件中获取原始属性数据?

  •  1
  • Giel Berkers  · 技术社区  · 8 年前

    我正在使用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() -语句,数据不存储在数据库中。因此,似乎存储库返回内存模型或类似的模型。

    有人能给我指出正确的方向吗?或者我是否使用了错误的方法来比较新输入的数据和已经存在的数据?

    1 回复  |  直到 8 年前
        1
  •  2
  •   SamirBoulil    8 年前

    有人能给我指出正确的方向吗?还是我用错了

    https://github.com/akeneo/pim-community-dev/blob/2.2/src/Pim/Bundle/CatalogBundle/Doctrine/ORM/Query/FindAttributesForFamily.php )。它的目的是直接从数据库中获取所需的数据(这将是原始值,因为在预保存时产品没有在数据库中刷新)

    推荐文章