Symfony 3.4.2 Akeneo – Can’t get the original attribute data in the akeneo.storage.pre_save event

I’m using akeneo.storage.pre_save to check if some attributes were changed. The problem is that repository always returns new data instead of old data from database. Does anyone got any ideas how to fix it? Or maybe it is another way to check if attribute value is changed?

/**
 * @param GenericEvent $event
 */
public function onPreSave(GenericEvent $event)
{
    /**
     * Array with fields, needed to be checked for updates
     */
    $lProjectVars = [
        'name_ro',
        'description_ro',
        'manufacturer_lp',
        'mt_hero_lp',
        'mt_theme_lp',
        'mt_collection_status',
        'mt_storeview_ro',
        'mt_ean_ro',
        'mt_picture1_ro',
        'mt_picture2_ro',
        'mt_picture3_ro',
        'mt_picture4_ro',
        'mt_picture5_ro',
        'mt_picture6_ro',
        'mt_picture7_ro',
        'mt_picture8_ro',
        'mt_picture9_ro'
    ];

    //get product with changed values from form
    $subject = $event->getSubject();
    if (!$subject instanceof ProductInterface || null == $this->requestStack->getCurrentRequest()) {
        // don't do anything if it's not a product OR not a HTTP request
        return;
    }

    //get product with old values from database
    $product = $this->productRepository->findOneByIdentifier($subject->getIdentifier());

    foreach ($subject->getAttributes() as $attribute) {
        if (in_array($attribute->getCode(), $lProjectVars)) {
            if ($subject->getValue($attribute->getCode()) !== $product->getValue($attribute->getCode())) {
                $this->propertySetter->setData($subject, "last_modification", date('Y-m-d'), ['locale' => null, 'scope' => null]);

                break;
            }
        }
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *