Skip to content

Instantly share code, notes, and snippets.

@radzserg
Last active December 31, 2015 13:59
Show Gist options
  • Save radzserg/7996591 to your computer and use it in GitHub Desktop.
Save radzserg/7996591 to your computer and use it in GitHub Desktop.
Yii CDbFixtureManager do not update sequence after loading data
<?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