Skip to content

Commit

Permalink
Add update + join support.
Browse files Browse the repository at this point in the history
This currently only works in MySQL. Additional work will be needed to
make it work with postgres + sqlite.
  • Loading branch information
markstory committed Jun 20, 2013
1 parent debc64b commit d31a750
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Cake/Database/Query.php
Expand Up @@ -291,7 +291,7 @@ protected function _traverseDelete(callable $visitor) {
* @return void
*/
protected function _traverseUpdate(callable $visitor) {
$parts = ['update', 'set', 'where'];
$parts = ['update', 'join', 'set', 'where'];
foreach ($parts as $name) {
call_user_func($visitor, $this->_parts[$name], $name);
}
Expand Down
26 changes: 26 additions & 0 deletions lib/Cake/Test/TestCase/Database/QueryTest.php
Expand Up @@ -1626,6 +1626,32 @@ public function testUpdateWithExpression() {
$this->assertCount(1, $result);
}

/**
* Test updates with joins.
*
* @return void
*/
public function testUpdateWithJoins() {
$query = new Query($this->connection);

$query->update('articles')
->set('title', 'New title')
->join([
'table' => 'authors',
'alias' => 'a',
'conditions' => 'author_id = a.id'
])
->where(['articles.id' => 1]);
$result = $query->sql(false);

$this->assertContains('UPDATE articles INNER JOIN authors a ON author_id = a.id', $result);
$this->assertContains('SET title = :', $result);
$this->assertContains('WHERE articles.id = :', $result);

$result = $query->execute();
$this->assertCount(1, $result);
}

/**
* You cannot call values() before insert() it causes all sorts of pain.
*
Expand Down

0 comments on commit d31a750

Please sign in to comment.