/** * Gets the foreign keys of a table * @param string $name Name of the table * @return array Array with ForeignKey objects */ protected function getTableFields($name) { $tableName = $this->connection->quoteIdentifier($name); $sql = 'SHOW FIELDS FROM ' . $tableName; $result = $this->connection->execute($sql); if (!$result) { throw new MysqlException('Could not find any fields for table ' . $name . '. Does it exist?'); } $fields = array(); foreach ($result as $data) { $field = new Field($data['Field'], $data['Type']); $field->setDefaultValue($data['Default']); $fields[] = $field; if (!$data['Key']) { continue; } if ($data['Key'] == 'PRI') { $field->setIsPrimaryKey(true); if ($data['Extra']) { $field->setIsAutoNumbering(true); $field->setDefaultValue(0); } } elseif ($data['Key'] == 'UNI') { $field->setIsUnique(true); } } return $fields; }
public function testGetPrimaryKeys() { $table = new Table('table'); $field1 = new Field('field1', 'type'); $field1->setIsPrimaryKey(true); $field2 = new Field('field2', 'type'); $field2->setIsPrimaryKey(true); $field3 = new Field('field3', 'type'); $table->addField($field1); $table->addField($field2); $table->addField($field3); $primaryKeys = $table->getPrimaryKeys(); $this->assertEquals(array('field1' => $field1, 'field2' => $field2), $primaryKeys); }
/** * @expectedException zibo\library\database\exception\DatabaseException */ public function testSetIsPrimaryKeyThrowsExceptionWhenNoBooleanPassed() { $field = new Field('name', 'type'); $field->setIsPrimaryKey('test'); }