I have an on-insert trigger that updates a product code in a table, based on a customer’s most recent code, stored in another table.
With symfony and Propel, I am doing the following (truncated unnecessary fields)
// actions.class.php
$lineitem = new LineItem();
$lineitem->setCustomerId( $customerId );
$thisProduct = ProductPeer::cacheProduct($orderDetails);
$lineitem->setCode($thisProduct->getCode()); // this should be fetching and setting the auto_code, inserted from the trigger.
$lineitem->save();
The cacheProduct function (part of ProductPeer) below
// ProductPeer.php
// auto_code field in DB left blank, ready to be written to by the trigger
// cacheProduct function
public static function cacheProduct($orderDetails)
{
$product = new Product();
$product->setName( $orderDetails->getName() );
$product->setCustomerCode( $orderDetails->getCustomerCode() );
//$product->setCode(); // this will be set by the TRIGGER on insert.
$product->save();
return $product->getId();
}
So the problem is that the product is being inserted, and the trigger is being run successfully, but Symfony is never aware of the newly inserted ‘code’ value. Running a fresh ProductPeer::retrieveByPK() still shows an empty ‘code’ field.
So in essence, at all points, $lineitem->getCode() returns null.
I’ve tried wrapping this in transactions, but it shouldn’t be necessary … and also there is potential for this to be in a loop and I don’t want to open and close transactions repeatedly unless absolutely necessary.
1) When does a MYSQL trigger actually run … on $xxx->save()? At the end of the script?
2) How can I access this value post-trigger, while still in the same script?
Solution found, thank you 🙂
Working with Propel version 1.4.8 or greater
Set
on your table schema, and rebuild your model: Example
(Be aware that this may have been introduced in a more recent version of Propel – check the version of your Propel plugin.)