diff --git a/idiorm.php b/idiorm.php index 4d454cb0..11488b55 100644 --- a/idiorm.php +++ b/idiorm.php @@ -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); } diff --git a/test/test_queries.php b/test/test_queries.php index 329ebc96..220b2022 100644 --- a/test/test_queries.php +++ b/test/test_queries.php @@ -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);