Skip to content

Commit

Permalink
Making joins also have a types param
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 2, 2013
1 parent 22c310b commit 694cd68
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/Model/Datasource/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function from($tables = [], $overwrite = false) {
return $this;
}

public function join($tables = null, $overwrite = false) {
public function join($tables = null, $types = [], $overwrite = false) {
if ($tables === null) {
return $this->_parts['join'];
}
Expand All @@ -230,7 +230,7 @@ public function join($tables = null, $overwrite = false) {
$t = array('table' => $t, 'conditions' => $this->newExpr());
}
if (!($t['conditions']) instanceof QueryExpression) {
$t['conditions'] = $this->newExpr()->add($t['conditions']);
$t['conditions'] = $this->newExpr()->add($t['conditions'], $types);
}
$joins[] = $t + ['type' => 'INNER', 'alias' => null];
}
Expand Down
88 changes: 51 additions & 37 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,41 @@ protected function _insertTwoRecords() {
return $result;
}

/**
* Auxiliary function to insert a couple rows in a newly created table containing dates
*
* @return void
**/
protected function _insertDateRecords() {
$table = 'CREATE TEMPORARY TABLE dates(id int, name varchar(50), posted datetime, visible char(1))';
$this->connection->execute($table);
$data = [
'id' => '1',
'name' => 'Chuck Norris',
'posted' => new \DateTime('2012-12-21 12:00'),
'visible' => 'Y'
];
$result = $this->connection->insert(
'dates',
$data,
['id' => 'integer', 'name' => 'string', 'posted' => 'datetime', 'visible' => 'string']
);

$result->bindValue(1, '2', 'integer');
$result->bindValue(2, 'Bruce Lee');
$result->bindValue(3, new \DateTime('2012-12-22 12:00'), 'datetime');
$result->bindValue(4, 'N');
$result->execute();

$result->bindValue(1, 3, 'integer');
$result->bindValue(2, 'Jet Li');
$result->bindValue(3, new \DateTime('2012-12-25 12:00'), 'datetime');
$result->bindValue(4, null);
$result->execute();

return $result;
}

/**
* Tests that it is possible to obtain expression results from a query
*
Expand Down Expand Up @@ -168,10 +203,12 @@ public function testSelectWithJoins() {
$this->assertEquals(array('title' => 'a title', 'name' => 'Chuck Norris'), $result->fetch('assoc'));
$this->assertEquals(array('title' => 'another title', 'name' => 'Bruce Lee'), $result->fetch('assoc'));

$result = $query->join('authors', true)->execute();
$result = $query->join('authors', [], true)->execute();
$this->assertCount(4, $result);

$result = $query->join([['table' => 'authors', 'type' => 'INNER', 'conditions' => 'author_id = 1']], true)->execute();
$result = $query->join([
['table' => 'authors', 'type' => 'INNER', 'conditions' => 'author_id = 1']
], [], true)->execute();
$this->assertCount(2, $result);
$this->assertEquals(array('title' => 'a title', 'name' => 'Chuck Norris'), $result->fetch('assoc'));
$this->assertEquals(array('title' => 'a title', 'name' => 'Bruce Lee'), $result->fetch('assoc'));
Expand All @@ -184,6 +221,7 @@ public function testSelectWithJoins() {
**/
public function testSelectWithJoinsConditions() {
$this->_insertTwoRecords();
$this->_insertDateRecords();
$query = new Query($this->connection);
$result = $query
->select(['title', 'name'])
Expand All @@ -202,6 +240,17 @@ public function testSelectWithJoinsConditions() {
->execute();
$this->assertEquals(array('title' => 'another title', 'name' => 'Chuck Norris'), $result->fetch('assoc'));
$this->assertEquals(array('title' => 'another title', 'name' => 'Bruce Lee'), $result->fetch('assoc'));

$query = new Query($this->connection);
$time = new \DateTime('2012-12-21 12:00');
$types = ['posted' => 'datetime'];
$result = $query
->select(['title', 'name' => 'd.name'])
->from('articles')
->join(['table' => 'dates', 'alias' => 'd', 'conditions' => ['posted' => $time]], $types)
->execute();
$this->assertEquals(array('title' => 'a title', 'name' => 'Chuck Norris'), $result->fetch('assoc'));
$this->assertEquals(array('title' => 'another title', 'name' => 'Chuck Norris'), $result->fetch('assoc'));
}

/**
Expand Down Expand Up @@ -312,41 +361,6 @@ public function testSelectWhereOperators() {
$this->assertCount(0, $result);
}

/**
* Auxiliary function to insert a couple rows in a newly created table containing dates
*
* @return void
**/
protected function _insertDateRecords() {
$table = 'CREATE TEMPORARY TABLE dates(id int, name varchar(50), posted datetime, visible char(1))';
$this->connection->execute($table);
$data = [
'id' => '1',
'name' => 'Chuck Norris',
'posted' => new \DateTime('2012-12-21 12:00'),
'visible' => 'Y'
];
$result = $this->connection->insert(
'dates',
$data,
['id' => 'integer', 'name' => 'string', 'posted' => 'datetime', 'visible' => 'string']
);

$result->bindValue(1, '2', 'integer');
$result->bindValue(2, 'Bruce Lee');
$result->bindValue(3, new \DateTime('2012-12-22 12:00'), 'datetime');
$result->bindValue(4, 'N');
$result->execute();

$result->bindValue(1, 3, 'integer');
$result->bindValue(2, 'Jet Li');
$result->bindValue(3, new \DateTime('2012-12-25 12:00'), 'datetime');
$result->bindValue(4, null);
$result->execute();

return $result;
}

/**
* Tests selecting with conditions and specifying types for those
*
Expand Down

0 comments on commit 694cd68

Please sign in to comment.