Skip to content

Commit

Permalink
Handle ambiguous column names when joining two tables
Browse files Browse the repository at this point in the history
  • Loading branch information
hellogerard authored and treffynnon committed Nov 12, 2012
1 parent 004bfac commit 9f9765d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Changelog
* Add support for `MIN`, `AVG`, `MAX` and `SUM` - closes issue #16
* Add `group_by_expr` - closes issue #24
* Add `set_expr` to allow database expressions to be set as ORM properties - closes issues #59 and #43 [[brianherbert](https://github.com/brianherbert)]
* Prevent ambiguous column names when joining tables - issue #66 [[hellogerard](https://github.com/hellogerard)]

#### 1.1.1 - release 2011-01-30

Expand Down
4 changes: 4 additions & 0 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,10 @@ protected function _add_where($fragment, $values=array()) {
* of the call to _quote_identifier
*/
protected function _add_simple_where($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}";
}
$column_name = $this->_quote_identifier($column_name);
return $this->_add_where("{$column_name} {$separator} ?", $value);
}
Expand Down
4 changes: 4 additions & 0 deletions test/test_queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal("Simple join", $expected);

ORM::for_table('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_one(5);
$expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` WHERE `widget`.`id` = '5' LIMIT 1";
Tester::check_equal("Simple join with where_id_is method", $expected);

ORM::for_table('widget')->inner_join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many();
$expected = "SELECT * FROM `widget` INNER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
Tester::check_equal("Inner join", $expected);
Expand Down

0 comments on commit 9f9765d

Please sign in to comment.