Browse Source

Adding leftJoin, rightJoin and innerJoin methods to the Query class

Jose Lorenzo Rodriguez 11 years ago
parent
commit
20d54fed77
1 changed files with 106 additions and 1 deletions
  1. 106 1
      src/Database/Query.php

+ 106 - 1
src/Database/Query.php

@@ -485,7 +485,7 @@ class Query implements ExpressionInterface, IteratorAggregate {
 			if (!is_array($t)) {
 				$t = ['table' => $t, 'conditions' => $this->newExpr()];
 			}
-			if (!($t['conditions']) instanceof ExpressionInterface) {
+			if (!($t['conditions'] instanceof ExpressionInterface)) {
 				$t['conditions'] = $this->newExpr()->add($t['conditions'], $types);
 			}
 			$alias = is_string($alias) ? $alias : null;
@@ -503,6 +503,110 @@ class Query implements ExpressionInterface, IteratorAggregate {
 	}
 
 /**
+ * Adds a single LEFT JOIN clause to the query.
+ *
+ * {{{
+ * // LEFT JOIN authors ON posts.author_id' = authors.id
+ * $query->leftJoin('authors', ['posts.author_id' = authors.id']);
+ * }}}
+ *
+ * You can pass an array in the first parameter if you need to alias
+ * the table for the join:
+ *
+ * {{{
+ * // LEFT JOIN authors a ON posts.author_id' = a.id
+ * $query->leftJoin(['a' => 'authors'], ['posts.author_id' = 'a.id']);
+ * }}}
+ *
+ * @param string|array $table The table to join with
+ * @param string|array|\Cake\Database\ExpressionInterface $conditions The conditions
+ * to use for joining.
+ * @param array $types a list of types associated to the conditions used for converting
+ * values to the corresponding database representation.
+ * @return $this
+ */
+	public function leftJoin($table, $conditions = [], $types = []) {
+		return $this->join($this->_makeJoin($table + ['type' => 'LEFT'], $conditions), $types);
+	}
+
+/**
+ * Adds a single RIGHT JOIN clause to the query.
+ *
+ * {{{
+ * // RIGHT JOIN authors ON posts.author_id' = authors.id
+ * $query->rightJoin('authors', ['posts.author_id' = authors.id']);
+ * }}}
+ *
+ * You can pass an array in the first parameter if you need to alias
+ * the table for the join:
+ *
+ * {{{
+ * // RIGHT JOIN authors a ON posts.author_id' = a.id
+ * $query->righJoin(['a' => 'authors'], ['posts.author_id' = 'a.id']);
+ * }}}
+ *
+ * @param string|array $table The table to join with
+ * @param string|array|\Cake\Database\ExpressionInterface $conditions The conditions
+ * to use for joining.
+ * @param array $types a list of types associated to the conditions used for converting
+ * values to the corresponding database representation.
+ * @return $this
+ */
+	public function rightJoin($table, $conditions = [], $types = []) {
+		return $this->join($this->_makeJoin($table + ['type' => 'RIGHT'], $conditions), $types);
+	}
+
+/**
+ * Adds a single INNER JOIN clause to the query.
+ *
+ * {{{
+ * // INNER JOIN authors ON posts.author_id' = authors.id
+ * $query->innerJoin('authors', ['posts.author_id' = authors.id']);
+ * }}}
+ *
+ * You can pass an array in the first parameter if you need to alias
+ * the table for the join:
+ *
+ * {{{
+ * // INNER JOIN authors a ON posts.author_id' = a.id
+ * $query->innerJoin(['a' => 'authors'], ['posts.author_id' = 'a.id']);
+ * }}}
+ *
+ * @param string|array $table The table to join with
+ * @param string|array|\Cake\Database\ExpressionInterface $conditions The conditions
+ * to use for joining.
+ * @param array $types a list of types associated to the conditions used for converting
+ * values to the corresponding database representation.
+ * @return $this
+ */
+	public function innerJoin($table, $conditions = [], $types = []) {
+		return $this->join($this->_makeJoin($table + ['type' => 'INNER'], $conditions), $types);
+	}
+
+/**
+ * Returns an array that can be passed to the join method describing a single join clause
+ *
+ * @param string|array $table The table to join with
+ * @param string|array|\Cake\Database\ExpressionInterface $conditions The conditions
+ * to use for joining.
+ * @return array
+ */
+	protected function _makeJoin($table, $conditions) {
+		$alias = $table;
+
+		if (is_array($table)) {
+			$alias = key($table);
+			$table = current($table);
+		}
+
+		return [
+			'table' => $table,
+			'alias' => $alias,
+			'conditions' => $conditions
+		];
+	}
+
+/**
  * Adds a condition or set of conditions to be used in the WHERE clause for this
  * query. Conditions can be expressed as an array of fields as keys with
  * comparison operators in it, the values for the array will be used for comparing
@@ -1221,6 +1325,7 @@ class Query implements ExpressionInterface, IteratorAggregate {
  * $expression = $query->newExpression('Table.column = Table2.column'); // Return a raw SQL expression
  * }}}
  *
+ * @param mixed $rawExpression A string, array or anything you want wrapped in a expression object
  * @return \Cake\Database\QueryExpression
  */
 	public function newExpr($rawExpression = null) {