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