Transaction Scope Issue

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');

					

		}

				

	}

I fixed this, for reference, I set the primary key using a massive-assigned array. The PK is an identity column so during the save it was clashing and returning the value I had set in the array, over the actual identity key. It was successfully saving the record however in both scenarios.

I think the save() needs to check the PK and throw a warning perhaps if you specify it during an isNewRecord=1.