![]() |
|
Snippets |
|
Sometimes a table needs to have non-autoincremented primary keys (for example, to be able to import data from other sources keeping the same id's for objects).
I use the following procedure:
DBDesigner -> Propel schema (config/schema.xml) through DBDesigner2Propel
Generate Doctrine schema (config/doctrine/schema.yml) through symfony doctrine-import.
The problem is that the resulting schema.yml no longer includes information about the primary keys, so doctrine-insert-sql and doctrine-build-model will create an id field as autoincremented primary key.
The solution is a small patch which doesn't change the default behaviour. Look at sfDoctrineSchemaPropelLoader.class.php around the line 52 and modify a few lines in the code to be as the following:
// columns foreach ($propelDatabaseSchema->getChildren($tableDesc) as $col_name => $columnDescription) { $docCol = new sfDoctrineColumnSchema($col_name, $columnDescription, true); if($col_name == 'id') { if($docCol->getColumnInfo()->get('autoIncrement') === true) { // We skip integer auto-increment primary keys, but we keep the rest continue; } } $class->addColumn($docCol); }
There is a bug in sfDoctrineColumnSchema.class.php which should be fixed too. The problem is that it is intersecting with the non-translated constraints instead the ones already translated from propel.
The fix is easy:
Original line:
if ($constraints = array_intersect_key($columnDescription, array_flip(self::$allowedConstraints)))
Change to this:
// Change to this: if ($constraints = array_intersect_key($this->columnInfo->getAll(), array_flip(self::$allowedConstraints)))