Background
I have an entity that's directly linked to Magento shipments with three fields:
- entity_id
- data
- shipment_id
Problem
This entity is created based on a condition in an observer on the sales_order_shipment_save_before
event. Something that looks like this:
if (condition) {
// Create my entity and keep going
}
else {
// Throw an exception and stop the Magento shipment creation
}
As you can see, if the condition is not matched, it must stop generating the Magento shipment. However, using this event, I cannot get the Magento shipment id using $shipment->getIncrementId()
, because it's a before event.
If I try to use the sales_order_shipment_save_after
event, I am able to get the increment id. However, I cannot stop the creation of the Magento shipment as it already has been created.
Temporary fix
A hacky way of fixing my issue is to get the next increment id by calling some code like this:
$sql = sprintf("SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = 'sales_flat_shipment' and table_schema = '%s'",
Mage::getConfig()->getNode('global/resources/default_setup/connection/dbname'));
$readConn = Mage::getSingleton('core/resource')->getConnection('core_read');
$results = $readConn->fetchAll($sql);
$nextIncrementId = $results[0]['AUTO_INCREMENT'];
But this could possibly run into a race condition.
So how can I fix my problem without running into a race condition?