Last active
December 31, 2015 13:59
-
-
Save radzserg/7996591 to your computer and use it in GitHub Desktop.
Yii CDbFixtureManager do not update sequence after loading data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function loadFixture($tableName) | |
{ | |
$fileName=$this->basePath.DIRECTORY_SEPARATOR.$tableName.'.php'; | |
if(!is_file($fileName)) | |
return false; | |
$rows=array(); | |
$schema=$this->getDbConnection()->getSchema(); | |
$builder=$schema->getCommandBuilder(); | |
$table=$schema->getTable($tableName); | |
foreach(require($fileName) as $alias=>$row) | |
{ | |
$builder->createInsertCommand($table,$row)->execute(); | |
$primaryKey=$table->primaryKey; | |
if($table->sequenceName!==null) | |
{ | |
if(is_string($primaryKey) && !isset($row[$primaryKey])) | |
$row[$primaryKey]=$builder->getLastInsertID($table); | |
elseif(is_array($primaryKey)) | |
{ | |
foreach($primaryKey as $pk) | |
{ | |
if(!isset($row[$pk])) | |
{ | |
$row[$pk]=$builder->getLastInsertID($table); | |
break; | |
} | |
} | |
} | |
// we need to update sequence value as well ! | |
$schema->resetSequence($table, $row[$primaryKey] + 1); | |
} | |
$rows[$alias]=$row; | |
} | |
return $rows; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment