summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Andrée <[email protected]>2013-08-14 14:44:20 +0200
committerVictor Andrée <[email protected]>2013-08-14 14:44:20 +0200
commit19e278c2f70e77390b0497867b397ee67f0747e8 (patch)
treebed8bd2d014243d1049a1c67b336d32a40062f59
parent01112f5ac14b24aed8038cc0ae99801007ef26b8 (diff)
Uses table alias in WHERE
If a table alias is used for the main table, it's not valid according to the SQL standard to refer to it by it's "real name" (see http://www.postgresql.org/docs/8.2/static/queries-table-expressions.html #QUERIES-TABLE-ALIASES). This causes problems when doing JOIN with a table alias for the "main table", since previous code always used table name. Now, we check for a table alias.
-rw-r--r--idiorm.php7
-rw-r--r--test/QueryBuilderTest.php6
2 files changed, 12 insertions, 1 deletions
diff --git a/idiorm.php b/idiorm.php
index 5b327a6..ad7b965 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -936,7 +936,12 @@
protected function _add_simple_condition($type, $column_name, $separator, $value) {
// Add the table name in case of ambiguous columns
if (count($this->_join_sources) > 0 && strpos($column_name, '.') === false) {
- $column_name = "{$this->_table_name}.{$column_name}";
+ $table = $this->_table_name;
+ if (!is_null($this->_table_alias)) {
+ $table = $this->_table_alias;
+ }
+
+ $column_name = "{$table}.{$column_name}";
}
$column_name = $this->_quote_identifier($column_name);
return $this->_add_condition($type, "{$column_name} {$separator} ?", $value);
diff --git a/test/QueryBuilderTest.php b/test/QueryBuilderTest.php
index bd888c0..a6e275b 100644
--- a/test/QueryBuilderTest.php
+++ b/test/QueryBuilderTest.php
@@ -373,6 +373,12 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, ORM::get_last_query());
}
+ public function testJonWithAliasesAndWhere() {
+ ORM::for_table('widget')->table_alias('w')->join('widget_handle', array('wh.widget_id', '=', 'w.id'), 'wh')->where_equal('id', 1)->find_many();
+ $expected = "SELECT * FROM `widget` `w` JOIN `widget_handle` `wh` ON `wh`.`widget_id` = `w`.`id` WHERE `w`.`id` = '1'";
+ $this->assertEquals($expected, ORM::get_last_query());
+ }
+
public function testJoinWithStringConstraint() {
ORM::for_table('widget')->join('widget_handle', "widget_handle.widget_id = widget.id")->find_many();
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON widget_handle.widget_id = widget.id";