From cc5a2db1024c79acc24188521e6249ef7fc764c2 Mon Sep 17 00:00:00 2001 From: Guangcheng Wei Date: Fri, 7 Nov 2014 15:49:22 +0800 Subject: [PATCH] Add ability to register a primary key. Pair with @LiGang9090. --- lib/Phactory/Sql/Blueprint.php | 4 +++ lib/Phactory/Sql/Phactory.php | 3 ++- lib/Phactory/Sql/Table.php | 8 ++++++ tests/Phactory/Sql/TableTest.php | 43 ++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/Phactory/Sql/TableTest.php diff --git a/lib/Phactory/Sql/Blueprint.php b/lib/Phactory/Sql/Blueprint.php index 7da5de5..2f18187 100644 --- a/lib/Phactory/Sql/Blueprint.php +++ b/lib/Phactory/Sql/Blueprint.php @@ -29,6 +29,10 @@ public function __construct($name, $defaults, $associations = array(), Phactory } } + public function setTablePrimaryKey($primary_key) { + $this->_table->setPrimaryKey($primary_key); + } + public function setDefaults($defaults) { $this->_defaults = $defaults; } diff --git a/lib/Phactory/Sql/Phactory.php b/lib/Phactory/Sql/Phactory.php index 6f52656..4453f54 100644 --- a/lib/Phactory/Sql/Phactory.php +++ b/lib/Phactory/Sql/Phactory.php @@ -48,12 +48,13 @@ public function getConnection() { * @param array $defaults key => value pairs of column => value, or a phactory_blueprint * @param array $associations array of phactory_associations */ - public function define($blueprint_name, $defaults = array(), $associations = array()) { + public function define($blueprint_name, $defaults = array(), $associations = array(), $primary_key = null) { if($defaults instanceof Blueprint) { $blueprint = $defaults; } else { $blueprint = new Blueprint($blueprint_name, $defaults, $associations, $this); } + $blueprint->setTablePrimaryKey($primary_key); $this->_blueprints[$blueprint_name] = $blueprint; } diff --git a/lib/Phactory/Sql/Table.php b/lib/Phactory/Sql/Table.php index 331cec1..b111450 100644 --- a/lib/Phactory/Sql/Table.php +++ b/lib/Phactory/Sql/Table.php @@ -6,6 +6,7 @@ class Table { protected $_singular; protected $_name; protected $_db_util; + protected $_primary_key; protected $_phactory; public function __construct($singular_name, $pluralize = true, Phactory $phactory) { @@ -19,6 +20,10 @@ public function __construct($singular_name, $pluralize = true, Phactory $phactor } } + public function setPrimaryKey($primary_key){ + return $this->_primary_key = $primary_key; + } + public function getName() { return $this->_name; } @@ -28,6 +33,9 @@ public function getSingularName() { } public function getPrimaryKey() { + if($this->_primary_key){ + return $this->_primary_key; + } return $this->_db_util->getPrimaryKey($this->_name); } diff --git a/tests/Phactory/Sql/TableTest.php b/tests/Phactory/Sql/TableTest.php new file mode 100644 index 0000000..eddb62d --- /dev/null +++ b/tests/Phactory/Sql/TableTest.php @@ -0,0 +1,43 @@ +pdo = new \PDO("sqlite:test.db"); + $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + + $this->pdo->exec("CREATE TABLE `users` ( id INTEGER PRIMARY KEY, name TEXT )"); + $this->pdo->exec("CREATE TABLE `settings` ( id INTEGER, name TEXT )"); + + $this->phactory = new Phactory($this->pdo); + } + + protected function tearDown() + { + $this->pdo->exec("DROP TABLE `settings`"); + $this->pdo->exec("DROP TABLE `users`"); + $this->phactory->reset(); + } + + public function testGetPrimaryKeyWhenPrimaryKeyExists() + { + $table = new Table('user', true, $this->phactory); + $this->assertEquals('id', $table->getPrimaryKey()); + } + + public function testGetPrimaryKeyWhenPrimaryKeyRegistered() + { + $table = new Table('setting', true, $this->phactory); + $table->setPrimaryKey('id'); + $this->assertEquals('id', $table->getPrimaryKey()); + } + + +} +?>