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)
使用symfony和Propel,我正在执行以下操作(截断不必要的字段)
// 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
下面是cacheProduct函数(ProductPeer的一部分)
// 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.
所以问题是正在插入产品,并且触发器正在成功运行,但Symfony从未意识到新插入的“代码”值。运行一个新的ProductPeer :: retrieveByPK()仍然显示一个空的'code'字段。
So in essence, at all points, $lineitem->getCode() returns null.
所以实质上,在所有点上,$ lineitem-> getCode()都返回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?
1)MYSQL触发器何时实际运行...在$ xxx-> save()?在剧本的最后? 2)如何在仍然在同一个脚本中的后触发器中访问此值?
Solution found, thank you :) Working with Propel version 1.4.8 or greater
找到解决方案,谢谢:)使用Propel 1.4.8或更高版本
1 个解决方案
#1
2
Set
reloadOnInsert="true"
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.)
(请注意,这可能是在更新版本的Propel中引入的 - 请检查Propel插件的版本。)
#1
2
Set
reloadOnInsert="true"
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.)
(请注意,这可能是在更新版本的Propel中引入的 - 请检查Propel插件的版本。)