Menu

After save isn't after commit: the event trap Magento 2 and Laravel share
📰

After save isn't after commit: the event trap Magento 2 and Laravel share

DEV Community: laravel·Nikita Sirenko·3 days ago
#2aFaTePY
#devtolaravel#devto#dev#transaction#product
Reading 0:00
15s threshold

Magento's catalog_product_save_after runs before the COMMIT. So does Laravel's saved() , as soon as the save sits inside DB::transaction() . Publish to a queue from either, and a fast consumer can read the old row.

What one save does in Magento

In Magento 2, $productRepository->save() isn't one UPDATE. It opens a transaction, 2 levels deep: a CatalogSearch plugin opens one, the EntityManager another. Then it fires catalog_product_save_before , writes the product and its attribute values, fires catalog_product_save_after , and only then commits. Stock data, URL rewrites and the quote refresh hang off catalog_product_save_after . To add behavior, you declare an observer in events.xml .

Laravel has the same shape without the XML: an Eloquent observer with saving() and saved() , attached to the model with #[ObservedBy] .

Before the commit

Publish to RabbitMQ from catalog_product_save_after and 2 things can go wrong. A fast consumer reads the product before the transaction ends and gets the old row.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.