Save only works if no processing is done

I have a mysterious situation with Save to the database (Yii 1.1.8 SQL Server 2008).

This is a console application, first i create a model and save() which creates a new record, then i process a data file - and within that function i use a different model and save() which also works fine. At the end i update some variables of the first model and call save(), and this is where the surprise happens:

  • if i run the data processing, the last save() does not write to the database. NOTE: the model is updated correctly, the save() return true, and even the trace shows the correct query which should have resulted in an update to the record in the database.

  • if i comment out the file processing loop, the final save() actually shows up in the database.

i also tried using ‘update()’ instead of save() (as you can see in the code snippet) but it behaves the same.

ANY ideas will be greatly welcome. this has been under investigation for days.




	public function run($args) {

		.

		.

		//write processing to import log

		$importLog = new ImportLog();

		$importLog->var1 = $value1;

		$importLog->var2 = $value2;

		$importLog->var3 = $value3;

		$importLog->save();


		// now do some processing

		$this->createDBImport();


		// update import table 

		$importLog->var1 = $newValue1;

		$importLog->var2 = $newValue2;

		$importLog->update(array('var1','var2'));

		var_dump($importLog);

	}


	private function createDBImport() {

		$output = $filename.'.TXT';

		if (($outh = fopen(Yii::app()->params->dbInputDir.$output, "w")) == FALSE) { 

			echo "unable to create output file\n";

			exit(0);

		}

		$rowNum = 0;

		if (($handle = fopen(Yii::app()->params->currentDir.$input, "r")) !== FALSE) {

			// --- this is the section i comment out to make the second model->save work /*

			while (($data = fgetcsv($handle, 1000, $dlmtr)) !== FALSE) {

				$rowNum++;

				//print_r($data);

				$row = $rowNum . $delim 

					. $data[1] . $delim

					. $data[2] . $delim

					. $data[3] . $delim

					. $data[4] . $delim

					. $data[5] . $delim

					. $data[6] . $delim

					. $data[7] . $delim

					. "\n"; 

				fwrite($outh, $row);

			}

			// --- this is the end of the commented out section */

			fclose($handle);

		}

		else {

			echo "failed to open file $input\n";

			exit(0);

		}

		fclose ($outh);

		// write stats with rowNum into import stats

		

		$importStats = new ImportStats();

		$importStats->var1 = $value1;

		$importStats->var2 = $value2;

		$importStats->var3 = $value3;

		$importStats->item_count = $rowNum;

		$importStats->save();

	}



Can someone please throw some ideas, what you would look at to try and identify this issue, i am out of ideas and this is clearly a processing issue having the command sent correctly and the database not executing it - NOT because of an error but something else.

I thought maybe its the console app that ends immediately after, so it might be cutting off the connection to the db before the query is sent, so i tried adding Yii::App()->end(); but the same results.