Skip to content

Commit

Permalink
First major success in using a Query object as a subquery
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 14, 2013
1 parent b675e98 commit bfd7a4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/Model/Datasource/Database/Query.php
Expand Up @@ -89,7 +89,7 @@ public function sql() {
if (!count($parts)) {
return;
}
if ($parts instanceof QueryExpression || $parts instanceof self) {
if ($parts instanceof QueryExpression) {
$parts = [$parts->sql()];
}
if (isset($this->_templates[$name])) {
Expand Down Expand Up @@ -433,7 +433,7 @@ protected function _bindParams($statement) {
* @return string
**/
public function __toString() {
return $this->sql();
return sprintf('(%s)', $this->sql());
}

}
23 changes: 23 additions & 0 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -1265,4 +1265,27 @@ public function testSelectOffset() {
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
}

public function testSuqueryInSelect() {
$this->_insertDateRecords();
$this->_insertTwoRecords();

$query = new Query($this->connection);
$subquery = (new Query($this->connection))
->select('name')
->from(['b' => 'authors'])
->where(['b.id = a.id']);
$result = $query
->select(['id', 'name' => $subquery])
->from(['a' => 'dates'])->execute();

$expected = [
['id' => 1, 'name' => 'Chuck Norris'],
['id' => 2, 'name' => 'Bruce Lee'],
['id' => 3, 'name' => null]
];
$this->assertEquals($expected, $result->fetchAll('assoc'));


This comment has been minimized.

Copy link
@markstory

markstory Jan 28, 2013

Member

Extra whitespace.

}

}

0 comments on commit bfd7a4b

Please sign in to comment.