*/
public function createTable($name, $options=array())
{
- $pk = isset($options['primaryKey']) && $options['primaryKey'] === false ? false : 'id';
$tableDefinition =
$this->componentFactory('TableDefinition', array($name, $this, $options));
+
+ if (isset($options['primaryKey'])) {
+ if ($options['primaryKey'] === false) {
+ $pk = false;
+ } else {
+ switch ($options['primaryKey']) {
+ case 'true':
+ case 't':
+ case 1:
+ case '1':
+ $pk = 'id';
+ break;
+
+ case 'false':
+ case 'f':
+ case 0:
+ case '0':
+ $pk = false;
+
+ default:
+ $pk = $options['primaryKey'];
+ }
+ }
+ } else {
+ $pk = 'id';
+ }
+
if ($pk != false) {
$tableDefinition->primaryKey($pk);
}
+
return $tableDefinition;
}
$this->fail("Expected exception for no pk");
}
- public function testCreateTableWithExplicitPk()
+ public function testCreateTableWithNamedPk()
+ {
+ $this->_createTestTable('sports', array('primaryKey' => 'sports_id'));
+
+ $sql = "SELECT sports_id FROM sports WHERE sports_id = 1";
+ $this->assertEquals(1, $this->_conn->selectValue($sql));
+
+ try {
+ $sql = "SELECT id FROM sports WHERE id = 1";
+ $this->assertNull($this->_conn->selectValue($sql));
+ } catch (Exception $e) {
+ return;
+ }
+ $this->fail("Expected exception for wrong pk name");
+ }
+
+ public function testCreateTableWithSeparatePk()
{
$table = $this->_conn->createTable('testings');
$table->column('foo', 'primaryKey');
$this->fail("Expected exception for no pk");
}
- public function testCreateTableWithExplicitPk()
+ public function testCreateTableWithNamedPk()
+ {
+ $this->_createTestTable('sports', array('primaryKey' => 'sports_id'));
+
+ $sql = "SELECT sports_id FROM sports WHERE sports_id = 1";
+ $this->assertEquals(1, $this->_conn->selectValue($sql));
+
+ try {
+ $sql = "SELECT id FROM sports WHERE id = 1";
+ $this->assertNull($this->_conn->selectValue($sql));
+ } catch (Exception $e) {
+ return;
+ }
+ $this->fail("Expected exception for wrong pk name");
+ }
+
+ public function testCreateTableWithSeparatePk()
{
$table = $this->_conn->createTable('testings');
$table->column('foo', 'primaryKey');
$this->fail("Expected exception for no pk");
}
- public function testCreateTableWithExplicitPk()
+ public function testCreateTableWithNamedPk()
+ {
+ $this->_createTestTable('sports', array('primaryKey' => 'sports_id'));
+
+ $sql = "SELECT sports_id FROM sports WHERE sports_id = 1";
+ $this->assertEquals(1, $this->_conn->selectValue($sql));
+
+ try {
+ $sql = "SELECT id FROM sports WHERE id = 1";
+ $this->assertNull($this->_conn->selectValue($sql));
+ } catch (Exception $e) {
+ return;
+ }
+ $this->fail("Expected exception for wrong pk name");
+ }
+
+ public function testCreateTableWithSeparatePk()
{
$table = $this->_conn->createTable('testings');
$table->column('foo', 'primaryKey');
$this->fail("Expected exception for no pk");
}
- public function testCreateTableWithExplicitPk()
+ public function testCreateTableWithNamedPk()
+ {
+ $this->_createTestTable('sports', array('primaryKey' => 'sports_id'));
+
+ $sql = "SELECT sports_id FROM sports WHERE sports_id = 1";
+ $this->assertEquals(1, $this->_conn->selectValue($sql));
+
+ try {
+ $sql = "SELECT id FROM sports WHERE id = 1";
+ $this->assertNull($this->_conn->selectValue($sql));
+ } catch (Exception $e) {
+ return;
+ }
+ $this->fail("Expected exception for wrong pk name");
+ }
+
+ public function testCreateTableWithSeparatePk()
{
$table = $this->_conn->createTable('testings');
$table->column('foo', 'primaryKey');