public function testConstraints() { $table = new Table('foo'); $table->addConstraint(new PrimaryKey(array('pk'))); $table->addConstraint(new ForeignKey($table, new Table('bar'))); $table->addConstraint(new Unique('uniq')); $this->assertCount(3, $table->getConstraints()); $constraints = $table->getConstraints(); $this->assertInstanceOf('Rentgen\\Database\\Constraint\\PrimaryKey', $constraints[0]); $this->assertInstanceOf('Rentgen\\Database\\Constraint\\ForeignKey', $constraints[1]); $this->assertInstanceOf('Rentgen\\Database\\Constraint\\Unique', $constraints[2]); }
public function testCreateTableWithNotAutoIncrementPrimaryKey() { $primaryKey = new PrimaryKey(); $primaryKey->disableAutoIncrement(); $table = new Table('foo'); $table->addConstraint($primaryKey); $this->getCreateTableCommand()->setTable($table)->execute(); $getTableCommand = new GetTableCommand(); $getTableCommand->setConnection($this->connection); $getTableCommand->setTableName('foo'); $tableInfo = $getTableCommand->execute(); $this->assertEquals('integer', $tableInfo->getColumn('foo_id')->getType()); }
/** * Load contsraints to table. * * @param Table $table A table. * * @return void */ private function loadConstraints(Table $table) { foreach ($this->getConstraints() as $constraint) { switch ($constraint['constraint_type']) { case 'FOREIGN KEY': // TODO Find a better way to define foreign key $foreignKey = new ForeignKey(new Table($constraint['table_name']), new Table($constraint['column_name'])); $foreignKey->setColumns($constraint['references_table']); $foreignKey->setReferencedColumns($constraint['references_field']); $table->addConstraint($foreignKey); break; case 'PRIMARY KEY': $table->addConstraint(new PrimaryKey($constraint['column_name'], $table)); break; case 'UNIQUE': $table->addConstraint(new Unique($constraint['column_name'], new Table($constraint['table_name']))); break; } } }