Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ambiguous column names when joining two tables #66

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,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 @@ -152,6 +152,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