Skip to content
Permalink
Browse files

Correcting use of association conditions for

BelongsToMany::cascadeDelete
  • Loading branch information...
lorenzo committed Dec 18, 2013
1 parent ec5a2aa commit f14fac548642cd7f13be532a8880bbb4d94d23b7
Showing with 26 additions and 18 deletions.
  1. +7 −6 Cake/ORM/Association/BelongsToMany.php
  2. +19 −12 Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
@@ -281,15 +281,17 @@ public function cascadeDelete(Entity $entity, $options = []) {
$conditions = array_combine($foreignKey, $entity->extract((array)$primaryKey));
}
$conditions = array_merge($conditions, $this->conditions());
$table = $this->junction();
$hasMany = $this->source()->association($table->alias());
if ($this->_cascadeCallbacks) {
foreach ($table->find('all')->where($conditions) as $related) {
foreach ($hasMany->find('all')->where($conditions) as $related) {
$table->delete($related, $options);
}
return true;
}
$conditions = array_merge($conditions, $hasMany->conditions());
return $table->deleteAll($conditions);
}
@@ -630,10 +632,9 @@ public function replaceLinks(Entity $sourceEntity, array $targetEntities, array
return $this->junction()->connection()->transactional(
function() use ($sourceEntity, $targetEntities, $primaryValue, $options) {
$foreignKey = (array)$this->foreignKey();
$belongsTo = $this->_junctionTable->association($this->target()->alias());
$existing = $this->_junctionTable->find('all')
->where(array_combine($foreignKey, $primaryValue))
->andWhere($belongsTo->conditions());
$hasMany = $this->source()->association($this->_junctionTable->alias());
$existing = $hasMany->find('all')
->where(array_combine($foreignKey, $primaryValue));
$jointEntities = $this->_collectJointEntities($sourceEntity, $targetEntities);
$inserts = $this->_diffLinks($existing, $jointEntities, $targetEntities);
@@ -619,16 +619,18 @@ public function testCascadeDelete() {
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'conditions' => ['Tags.name' => 'foo'],
'sort' => ['id' => 'ASC'],
];
$association = new BelongsToMany('Tags', $config);
$association->junction($articleTag);
$this->article
->association($articleTag->alias())
->conditions(['click_count' => 3]);
$articleTag->expects($this->once())
->method('deleteAll')
->with([
'Tags.name' => 'foo',
'click_count' => 3,
'article_id' => 1
]);
@@ -646,11 +648,13 @@ public function testCascadeDeleteWithCallbacks() {
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'conditions' => ['Tags.name' => 'foo'],
'cascadeCallbacks' => true,
];
$association = new BelongsToMany('Tag', $config);
$association->junction($articleTag);
$this->article
->association($articleTag->alias())
->conditions(['click_count' => 3]);
$articleTagOne = new Entity(['article_id' => 1, 'tag_id' => 2]);
$articleTagTwo = new Entity(['article_id' => 1, 'tag_id' => 4]);
@@ -660,9 +664,13 @@ public function testCascadeDeleteWithCallbacks() {
]);
$query = $this->getMock('\Cake\ORM\Query', [], [], '', false);
$query->expects($this->once())
$query->expects($this->at(0))
->method('where')
->with(['click_count' => 3])
->will($this->returnSelf());
$query->expects($this->at(1))
->method('where')
->with(['Tags.name' => 'foo', 'article_id' => 1])
->with(['article_id' => 1])
->will($this->returnSelf());
$query->expects($this->any())
@@ -991,9 +999,8 @@ public function testReplaceLinkSuccess() {
['tags', $config]
);
$assoc
->junction()
->association('tags')
$this->article
->association('ArticlesTags')
->conditions(['foo' => 1]);
$query1 = $this->getMock(
@@ -1006,13 +1013,13 @@ public function testReplaceLinkSuccess() {
->with('all')
->will($this->returnValue($query1));
$query1->expects($this->once())
$query1->expects($this->at(0))
->method('where')
->with(['article_id' => 1])
->with(['foo' => 1])
->will($this->returnSelf());
$query1->expects($this->at(1))
->method('andWhere')
->with(['foo' => 1])
->method('where')
->with(['article_id' => 1])
->will($this->returnSelf());
$existing = [

0 comments on commit f14fac5

Please sign in to comment.
You can’t perform that action at this time.