This example shows my problem, it is importing a text file. For each line of the file I need to insert data into several tables wrapped in a transaction. The tables are InnoDB. I have an issue where the INNER AR object “UwLocalPolicy” does not seem to return a new primary key after the call to ‘->save()’. Any ideas ?? Thanks, Peter
// Outer Controller code
class PageController extends CController
{
public fuction actionImport()
{
$dbTrans = Yii::app()->db->beginTransaction();
try
{
// Loop through lines in document being imported
foreach($row = 1; $row <= 10; $row++)
{
// Create policy info per line in file
$oLPolicy = new UwLocalPolicy();
$params = array(
'country_id' => $country_id ,
'servicing_company_id' => $servicing_company_id ,
'currency_id' => $policy_currency_id
);
$returnCode = $oLPolicy->createUpdateDraft(
$transaction_type
, $params
);
}
$dbTrans->commit();
}
catch (Exception $oEx)
{
$dbTrans->rollback();
}
}
}
// Inner CActiveRecord class
class UwLocalPolicy extends CActiveRecord
{
// Primary Key = local_policy_id
// Ar stuff ...
// Custom ADD/UPDATE function
public function createUpdateDraft (
$transaction_type
, $params = array()
)
{
// Perform some checks
if ($this->save())
{
$local_policy_id = $this->local_policy_id;
// PROBLEM $this->local_policy_id is always empty :(
// Can you pass the transaction context from outer to inner code? Should think so as it is held on the db connection ???
// Add child table dependent records
$oUwLocalPolicyLines = new UwLocalPolicyLines();
$oUwLocalPolicyLines->local_policy_id = $local_policy_id;
$oUwLocalPolicyLines->desc_text = 'Test Line info for Policy';
if (!$oUwLocalPolicyLines->save())
throw new Exception('Local Policy Line save failed');
}
else throw new Exception('Local Policy save failed');
}
}