/** * Returns the SQL for the primary key of a Table object * * @param Table $table * * @return string */ public function getPrimaryKeyDDL(Table $table) { if ($table->hasPrimaryKey()) { $keys = $table->getPrimaryKey(); //MySQL throws an 'Incorrect table definition; there can be only one auto column and it must be defined as a key' //if the primary key consists of multiple columns and if the first is not the autoIncrement one. So //this push the autoIncrement column to the first position if its not already. $autoIncrement = $table->getAutoIncrementPrimaryKey(); if ($autoIncrement && $keys[0] != $autoIncrement) { $idx = array_search($autoIncrement, $keys); if ($idx !== false) { unset($keys[$idx]); array_unshift($keys, $autoIncrement); } } return 'PRIMARY KEY (' . $this->getColumnListDDL($keys) . ')'; } }
public function testGetAutoIncrementPrimaryKey() { $column1 = $this->getColumnMock('id', array('primary' => true, 'auto_increment' => true)); $column2 = $this->getColumnMock('title'); $column3 = $this->getColumnMock('isbn'); $table = new Table(); $table->setIdMethod('native'); $table->addColumn($column1); $table->addColumn($column2); $table->addColumn($column3); $this->assertCount(1, $table->getPrimaryKey()); $this->assertTrue($table->hasPrimaryKey()); $this->assertTrue($table->hasAutoIncrementPrimaryKey()); $this->assertSame($column1, $table->getAutoIncrementPrimaryKey()); }