Browse Source

Proxying the deleteAll method from the Association class

Jose Lorenzo Rodriguez 12 years ago
parent
commit
a37725f860
2 changed files with 36 additions and 3 deletions
  1. 22 3
      src/ORM/Association.php
  2. 14 0
      tests/TestCase/ORM/AssociationProxyTest.php

+ 22 - 3
src/ORM/Association.php

@@ -481,7 +481,7 @@ abstract class Association {
 	}
 
 /**
- * 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,12 +491,31 @@ abstract class Association {
  */
 	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
  *

+ 14 - 0
tests/TestCase/ORM/AssociationProxyTest.php

@@ -77,4 +77,18 @@ class AssociationProxyTest extends TestCase {
 		$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);
+	}
+
 }