Skip to content

Commit

Permalink
Proxying the deleteAll method from the Association class
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 11, 2014
1 parent 330088e commit a37725f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/ORM/Association.php
Expand Up @@ -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()
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/ORM/AssociationProxyTest.php
Expand Up @@ -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);
}

}

0 comments on commit a37725f

Please sign in to comment.