summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Matthews <[email protected]>2010-10-25 20:21:52 +0100
committerJamie Matthews <[email protected]>2010-10-25 20:21:52 +0100
commit011b55119b56023224781be01d27043c5a1454ab (patch)
tree43719d69de51fb0f9cf6b0583ba53b9066d63919
parentb54973b4e0f157c79b2d0e2062d233ba2260b1a8 (diff)
Add table_alias method to provide ability to alias the main table in SELECT queries
-rw-r--r--idiorm.php17
-rw-r--r--test/test_queries.php3
2 files changed, 19 insertions, 1 deletions
diff --git a/idiorm.php b/idiorm.php
index c65fdd5..6087705 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -80,6 +80,9 @@
// The name of the table the current ORM instance is associated with
protected $_table_name;
+ // Alias for the table to be used in SELECT queries
+ protected $_table_alias = null;
+
// Values to be bound to the query
protected $_values = array();
@@ -362,6 +365,14 @@
}
/**
+ * Add a an alias for the main table to be used in SELECT queries
+ */
+ public function table_alias($alias) {
+ $this->_table_alias = $alias;
+ return $this;
+ }
+
+ /**
* Internal method to add an unquoted expression to the set
* of columns returned by the SELECT query. The second optional
* argument is the alias to return the expression as.
@@ -668,7 +679,11 @@
*/
protected function _build_select_start() {
$result_columns = join(', ', $this->_result_columns);
- return "SELECT {$result_columns} FROM " . $this->_quote_identifier($this->_table_name);
+ $fragment = "SELECT {$result_columns} FROM " . $this->_quote_identifier($this->_table_name);
+ if (!is_null($this->_table_alias)) {
+ $fragment .= " " . $this->_quote_identifier($this->_table_alias);
+ }
+ return $fragment;
}
/**
diff --git a/test/test_queries.php b/test/test_queries.php
index d72c101..807eed6 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -147,6 +147,9 @@
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` JOIN `widget_nozzle` ON `widget_nozzle`.`widget_id` = `widget`.`id`";
Tester::check_equal("Multiple join sources", $expected);
+ ORM::for_table('widget')->table_alias('w')->find_many();
+ $expected = "SELECT * FROM `widget` `w`";
+
ORM::for_table('widget')->join('widget_handle', array('wh.widget_id', '=', 'widget.id'), 'wh')->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` `wh` ON `wh`.`widget_id` = `widget`.`id`";
Tester::check_equal("Join with alias", $expected);