diff options
author | Jamie Matthews <[email protected]> | 2010-10-25 18:28:03 +0100 |
---|---|---|
committer | Jamie Matthews <[email protected]> | 2010-10-25 18:28:03 +0100 |
commit | b513f1e3ea3f6edb9d781dcd32ceb1d1497c60bf (patch) | |
tree | 9653e875b0267712257dc061c30e0b285b0cd400 | |
parent | 12d257077bca18b30f759da20161c929675bd04a (diff) |
Initial work on joins. Basic test, join and _add_join methods
-rw-r--r-- | idiorm.php | 64 | ||||
-rw-r--r-- | test/test_queries.php | 4 |
2 files changed, 68 insertions, 0 deletions
@@ -89,6 +89,9 @@ // Are we using the default result column or have these been manually changed? protected $_using_default_result_columns = true; + // Join sources + protected $_join_sources = array(); + // Is this a raw query? protected $_is_raw_query = false; @@ -397,6 +400,55 @@ } /** + * Internal method to add a JOIN source to the query. + * + * The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this + * will be prepended to JOIN. + * + * The table should be the name of the table to join to. + * + * The constraint should be an array with three elements: + * + * first_column, operator, second_column + * + * Example: array('user.id', '=', 'profile.user_id') + * + * will compile to + * + * ON `user`.`id` = `profile`.`user_id` + * + * The final (optional) argument specifies an alias for the joined table. + */ + protected function _add_join($join_operator, $table, $constraint, $table_alias=null) { + + $join_operator = trim("{$join_operator} JOIN"); + + $table = $this->_quote_identifier($table); + + // Add table alias if present + if (!is_null($table_alias)) { + $table_alias = $this->_quote_identifier($table_alias); + $table .= " AS {$table_alias}"; + } + + // Build the constraint + list($first_column, $operator, $second_column) = $constraint; + $first_column = $this->_quote_identifier($first_column); + $second_column = $this->_quote_identifier($second_column); + $constraint = "{$first_column} {$operator} {$second_column}"; + + $this->_join_sources[] = "{$join_operator} {$table} ON {$constraint}"; + return $this; + } + + /** + * Add a simple JOIN source to the query + */ + public function join($table, $constraint, $table_alias=null) { + return $this->_add_join("", $table, $constraint, $table_alias); + } + + /** * Internal method to add a WHERE condition to the query */ protected function _add_where($fragment, $values) { @@ -571,6 +623,7 @@ // the results of calling each separate builder method. return $this->_join_if_not_empty(" ", array( $this->_build_select_start(), + $this->_build_join(), $this->_build_where(), $this->_build_order_by(), $this->_build_limit(), @@ -587,6 +640,17 @@ } /** + * Build the JOIN sources + */ + protected function _build_join() { + if (count($this->_join_sources) === 0) { + return ''; + } + + return join(" ", $this->_join_sources); + } + + /** * Build the WHERE clause(s) */ protected function _build_where() { diff --git a/test/test_queries.php b/test/test_queries.php index c2247bf..ea7ae44 100644 --- a/test/test_queries.php +++ b/test/test_queries.php @@ -120,6 +120,10 @@ $expected = "SELECT COUNT(*) AS `count` FROM `widget`"; Tester::check_equal("Literal expression in result columns", $expected); + ORM::for_table('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->find_many(); + $expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; + Tester::check_equal("Simple join", $expected); + $widget = ORM::for_table('widget')->create(); $widget->name = "Fred"; $widget->age = 10; |