Yii Framework Forum: Transaction Scope Issue - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Transaction Scope Issue using transactions across objects Rate Topic: -----

#1 User is offline   Peter @ Liverpool 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 17-March 12

  Posted 30 December 2012 - 08:33 AM

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

0

#2 User is offline   Peter @ Liverpool 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 17-March 12

Posted 12 January 2013 - 10:33 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users