From a37725f860f5146deb2992ad7535a5890e93c67a Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 11 Apr 2014 11:13:11 +0200 Subject: [PATCH] Proxying the deleteAll method from the Association class --- src/ORM/Association.php | 25 ++++++++++++++++++--- tests/TestCase/ORM/AssociationProxyTest.php | 14 ++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/ORM/Association.php b/src/ORM/Association.php index c25cb27d767..588634425b8 100644 --- a/src/ORM/Association.php +++ b/src/ORM/Association.php @@ -481,7 +481,7 @@ public function find($type = 'all', $options = []) { } /** - * Proxies the update to the target table's updateAll method + * Proxies the update operation to the target table's updateAll method * * @param array $fields A hash of field => new value. * @param mixed $conditions Conditions to be used, accepts anything Query::where() @@ -491,11 +491,30 @@ public function find($type = 'all', $options = []) { */ public function updateAll($fields, $conditions) { $target = $this->target(); - $expression = $target->query()->newExpr(); - $expression->add($this->conditions())->add($conditions); + $expression = $target->query() + ->where($this->conditions()) + ->where($conditions) + ->clause('where'); return $target->updateAll($fields, $expression); } +/** + * Proxies the delete operation to the target table's deleteAll method + * + * @param mixed $conditions Conditions to be used, accepts anything Query::where() + * can take. + * @return boolean Success Returns true if one or more rows are affected. + * @see \Cake\ORM\Table::delteAll() + */ + public function deleteAll($conditions) { + $target = $this->target(); + $expression = $target->query() + ->where($this->conditions()) + ->where($conditions) + ->clause('where'); + return $target->deleteAll($expression); + } + /** * Triggers beforeFind on the target table for the query this association is * attaching to diff --git a/tests/TestCase/ORM/AssociationProxyTest.php b/tests/TestCase/ORM/AssociationProxyTest.php index a838f9559a1..0dfc5ca6e2a 100644 --- a/tests/TestCase/ORM/AssociationProxyTest.php +++ b/tests/TestCase/ORM/AssociationProxyTest.php @@ -77,4 +77,18 @@ public function testUpdateAllFromAssociation() { $this->assertEquals(3, $changed); } +/** + * Tests that the proxied deleteAll preserves conditions set for the association + * + * @return void + */ + public function testDeleteAllFromAssociation() { + $articles = TableRegistry::get('articles'); + $comments = TableRegistry::get('comments'); + $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]); + $articles->comments->deleteAll(['article_id' => 1]); + $remaining = $comments->find()->where(['article_id' => 1])->count(); + $this->assertEquals(1, $remaining); + } + }