cascadeDelete() public method

Clear out the data in the junction table for a given entity.
public cascadeDelete ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : boolean
$entity Cake\Datasource\EntityInterface The entity that started the cascading delete.
$options array The options for the original delete.
return boolean Success.
Beispiel #1
0
 /**
  * Test cascading deletes with callbacks.
  *
  * @return void
  */
 public function testCascadeDeleteWithCallbacks()
 {
     $articleTag = TableRegistry::get('ArticlesTags');
     $config = ['sourceTable' => $this->article, 'targetTable' => $this->tag, 'cascadeCallbacks' => true];
     $association = new BelongsToMany('Tag', $config);
     $association->junction($articleTag);
     $this->article->association($articleTag->alias());
     $counter = $this->getMockBuilder('StdClass')->setMethods(['__invoke'])->getMock();
     $counter->expects($this->exactly(2))->method('__invoke');
     $articleTag->eventManager()->on('Model.beforeDelete', $counter);
     $this->assertEquals(2, $articleTag->find()->where(['article_id' => 1])->count());
     $entity = new Entity(['id' => 1, 'name' => 'PHP']);
     $association->cascadeDelete($entity);
     $this->assertEquals(0, $articleTag->find()->where(['article_id' => 1])->count());
 }
 /**
  * Test cascading deletes with callbacks.
  *
  * @return void
  */
 public function testCascadeDeleteWithCallbacks()
 {
     $articleTag = $this->getMock('Cake\\ORM\\Table', ['find', 'delete'], []);
     $config = ['sourceTable' => $this->article, 'targetTable' => $this->tag, '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]);
     $iterator = new \ArrayIterator([$articleTagOne, $articleTagTwo]);
     $query = $this->getMock('\\Cake\\ORM\\Query', [], [], '', false);
     $query->expects($this->at(0))->method('where')->with(['click_count' => 3])->will($this->returnSelf());
     $query->expects($this->at(1))->method('where')->with(['article_id' => 1])->will($this->returnSelf());
     $query->expects($this->any())->method('getIterator')->will($this->returnValue($iterator));
     $articleTag->expects($this->once())->method('find')->will($this->returnValue($query));
     $articleTag->expects($this->at(1))->method('delete')->with($articleTagOne, []);
     $articleTag->expects($this->at(2))->method('delete')->with($articleTagTwo, []);
     $articleTag->expects($this->never())->method('deleteAll');
     $entity = new Entity(['id' => 1, 'name' => 'PHP']);
     $association->cascadeDelete($entity);
 }