summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Matthews <[email protected]>2010-10-07 02:06:07 +0100
committerJamie Matthews <[email protected]>2010-10-07 02:06:07 +0100
commit93481ff9f903b3c979913e967e49bd09f514e924 (patch)
tree44ed2070311cd40989b1a7b2f1aecf772926613e
parent69bf5735df7360264ad1763fc5bc303411bd8f6c (diff)
Add use_id_column method to allow specifying the ID column to use on a per-instance basis
-rw-r--r--idiorm.php24
-rw-r--r--test/test_queries.php12
2 files changed, 34 insertions, 2 deletions
diff --git a/idiorm.php b/idiorm.php
index 65e9189..161b086 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -123,6 +123,10 @@
// Are we updating or inserting?
protected $_update_or_insert = self::UPDATE;
+ // Name of the column to use as the primary key for
+ // this instance only. Overrides the config settings.
+ protected $_instance_id_column = null;
+
// ---------------------- //
// --- STATIC METHODS --- //
// ---------------------- //
@@ -219,6 +223,19 @@
}
/**
+ * Specify the ID column to use for this instance or array of instances only.
+ * This overrides the id_column and id_column_overrides settings.
+ *
+ * This is mostly useful for libraries built on top of Idiorm, and will
+ * not normally be used in manually built queries. If you don't know why
+ * you would want to use this, you should probably just ignore it.
+ */
+ public function use_id_column($id_column) {
+ $this->_instance_id_column = $id_column;
+ return $this;
+ }
+
+ /**
* Tell the ORM that you are expecting a single result
* back from your query, and execute it. Will return
* a single instance of the ORM class, or false if no
@@ -234,7 +251,7 @@
$this->_find_type = self::FIND_ONE;
$statement = $this->_run();
$result = $statement->fetch(PDO::FETCH_ASSOC);
- return $result ? self::for_table($this->_table_name)->hydrate($result) : $result;
+ return $result ? self::for_table($this->_table_name)->use_id_column($this->_instance_id_column)->hydrate($result) : $result;
}
/**
@@ -248,7 +265,7 @@
$statement = $this->_run();
$instances = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
- $instances[] = self::for_table($this->_table_name)->hydrate($row);
+ $instances[] = self::for_table($this->_table_name)->use_id_column($this->_instance_id_column)->hydrate($row);
}
return $instances;
}
@@ -596,6 +613,9 @@
* the primary key ID of the row.
*/
protected function _get_id_column_name() {
+ if (!is_null($this->_instance_id_column)) {
+ return $this->_instance_id_column;
+ }
if (isset(self::$_config['id_column_overrides'][$this->_table_name])) {
return self::$_config['id_column_overrides'][$this->_table_name];
} else {
diff --git a/test/test_queries.php b/test/test_queries.php
index 013b897..a7fbafa 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -136,5 +136,17 @@
$expected = 'SELECT * FROM `widget_nozzle` WHERE `primary_key` = "5"';
Tester::check_equal("Setting: id_column_overrides, third test", $expected);
+ ORM::for_table('widget')->use_id_column('new_id')->find_one(5);
+ $expected = 'SELECT * FROM `widget` WHERE `new_id` = "5"';
+ Tester::check_equal("Instance ID column, first test", $expected);
+
+ ORM::for_table('widget_handle')->use_id_column('new_id')->find_one(5);
+ $expected = 'SELECT * FROM `widget_handle` WHERE `new_id` = "5"';
+ Tester::check_equal("Instance ID column, second test", $expected);
+
+ ORM::for_table('widget_nozzle')->use_id_column('new_id')->find_one(5);
+ $expected = 'SELECT * FROM `widget_nozzle` WHERE `new_id` = "5"';
+ Tester::check_equal("Instance ID column, third test", $expected);
+
Tester::report();
?>