Skip to content
Permalink
Browse files

Fixing code and adding test for leftJoin

  • Loading branch information...
lorenzo committed Jul 18, 2014
1 parent 20d54fe commit f3941b9a91f815a625ad9da531dc305897f354fd
Showing with 35 additions and 7 deletions.
  1. +10 −7 src/Database/Query.php
  2. +25 −0 tests/TestCase/Database/QueryTest.php
@@ -526,7 +526,7 @@ public function join($tables = null, $types = [], $overwrite = false) {
* @return $this
*/
public function leftJoin($table, $conditions = [], $types = []) {
return $this->join($this->_makeJoin($table + ['type' => 'LEFT'], $conditions), $types);
return $this->join($this->_makeJoin($table, $conditions, 'LEFT'), $types);
}
/**
@@ -553,7 +553,7 @@ public function leftJoin($table, $conditions = [], $types = []) {
* @return $this
*/
public function rightJoin($table, $conditions = [], $types = []) {
return $this->join($this->_makeJoin($table + ['type' => 'RIGHT'], $conditions), $types);
return $this->join($this->_makeJoin($table, $conditions, 'RIGHT'), $types);
}
/**
@@ -580,7 +580,7 @@ public function rightJoin($table, $conditions = [], $types = []) {
* @return $this
*/
public function innerJoin($table, $conditions = [], $types = []) {
return $this->join($this->_makeJoin($table + ['type' => 'INNER'], $conditions), $types);
return $this->join($this->_makeJoin($table, $conditions, 'INNER'), $types);
}
/**
@@ -589,9 +589,10 @@ public function innerJoin($table, $conditions = [], $types = []) {
* @param string|array $table The table to join with
* @param string|array|\Cake\Database\ExpressionInterface $conditions The conditions
* to use for joining.
* @param string $type the join type to use
* @return array
*/
protected function _makeJoin($table, $conditions) {
protected function _makeJoin($table, $conditions, $type) {
$alias = $table;
if (is_array($table)) {
@@ -600,9 +601,11 @@ protected function _makeJoin($table, $conditions) {
}
return [
'table' => $table,
'alias' => $alias,
'conditions' => $conditions
$alias => [
'table' => $table,
'conditions' => $conditions,
'type' => $type
]
];
}
@@ -279,6 +279,31 @@ public function testSelectAliasedJoins() {
$this->assertEquals(array('title' => 'First Article', 'name' => 'First Comment for First Article'), $result->fetch('assoc'));
}
/**
* Tests the leftJoin method
*
* @return void
*/
public function testSelectLeftJoin() {
$query = new Query($this->connection);
$time = new \DateTime('2007-03-18 10:45:23');
$types = ['created' => 'datetime'];
$result = $query
->select(['title', 'name' => 'c.comment'])
->from('articles')
->leftJoin(['c' => 'comments'], ['created <' => $time], $types)
->execute();
$this->assertEquals(array('title' => 'First Article', 'name' => null), $result->fetch('assoc'));
$query = new Query($this->connection);
$result = $query
->select(['title', 'name' => 'c.comment'])
->from('articles')
->leftJoin(['c' => 'comments'], ['created >' => $time], $types)
->execute();
$this->assertEquals(array('title' => 'First Article', 'name' => 'Second Comment for First Article'), $result->fetch('assoc'));
}
/**
* Tests it is possible to filter a query by using simple AND joined conditions
*

0 comments on commit f3941b9

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