From d31a75055c6ff7b5633af2a9971cc47ae4f9067e Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 19 Jun 2013 21:28:35 -0400 Subject: [PATCH] Add update + join support. This currently only works in MySQL. Additional work will be needed to make it work with postgres + sqlite. --- lib/Cake/Database/Query.php | 2 +- lib/Cake/Test/TestCase/Database/QueryTest.php | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Database/Query.php b/lib/Cake/Database/Query.php index 47b35d298b7..ebad32e3e3e 100644 --- a/lib/Cake/Database/Query.php +++ b/lib/Cake/Database/Query.php @@ -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); } diff --git a/lib/Cake/Test/TestCase/Database/QueryTest.php b/lib/Cake/Test/TestCase/Database/QueryTest.php index 80129a8e31a..233b88ba597 100644 --- a/lib/Cake/Test/TestCase/Database/QueryTest.php +++ b/lib/Cake/Test/TestCase/Database/QueryTest.php @@ -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. *