Skip to content
Permalink
Browse files

Adding leftJoin, rightJoin and innerJoin methods to the Query class

  • Loading branch information...
lorenzo committed Jul 18, 2014
1 parent 1b44f4b commit 20d54fed77ae7396fdd6824ed78079cd2466af53
Showing with 106 additions and 1 deletion.
  1. +106 −1 src/Database/Query.php
@@ -485,7 +485,7 @@ public function join($tables = null, $types = [], $overwrite = false) {
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;
@@ -502,6 +502,110 @@ public function join($tables = null, $types = [], $overwrite = false) {
return $this;
}
/**
* 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
@@ -1221,6 +1325,7 @@ public function type() {
* $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) {

0 comments on commit 20d54fe

Please sign in to comment.
You can’t perform that action at this time.