/**
  * test cascading deletes.
  *
  * @return void
  */
 public function testCascadeDelete()
 {
     $mockOne = $this->getMockBuilder('Cake\\ORM\\Association\\BelongsTo')->setConstructorArgs([''])->getMock();
     $mockTwo = $this->getMockBuilder('Cake\\ORM\\Association\\HasMany')->setConstructorArgs([''])->getMock();
     $entity = new Entity();
     $options = ['option' => 'value'];
     $this->associations->add('One', $mockOne);
     $this->associations->add('Two', $mockTwo);
     $mockOne->expects($this->once())->method('cascadeDelete')->with($entity, $options);
     $mockTwo->expects($this->once())->method('cascadeDelete')->with($entity, $options);
     $this->assertNull($this->associations->cascadeDelete($entity, $options));
 }
Example #2
0
 /**
  * Perform the delete operation.
  *
  * Will delete the entity provided. Will remove rows from any
  * dependent associations, and clear out join tables for BelongsToMany associations.
  *
  * @param \Cake\Datasource\EntityInterface $entity The entity to delete.
  * @param \ArrayObject $options The options for the delete.
  * @throws \InvalidArgumentException if there are no primary key values of the
  * passed entity
  * @return bool success
  */
 protected function _processDelete($entity, $options)
 {
     if ($entity->isNew()) {
         return false;
     }
     $primaryKey = (array) $this->primaryKey();
     if (!$entity->has($primaryKey)) {
         $msg = 'Deleting requires all primary key values.';
         throw new InvalidArgumentException($msg);
     }
     if ($options['checkRules'] && !$this->checkRules($entity, RulesChecker::DELETE, $options)) {
         return false;
     }
     $event = $this->dispatchEvent('Model.beforeDelete', ['entity' => $entity, 'options' => $options]);
     if ($event->isStopped()) {
         return $event->result;
     }
     $this->_associations->cascadeDelete($entity, ['_primary' => false] + $options->getArrayCopy());
     $query = $this->query();
     $conditions = (array) $entity->extract($primaryKey);
     $statement = $query->delete()->where($conditions)->execute();
     $success = $statement->rowCount() > 0;
     if (!$success) {
         return $success;
     }
     $this->dispatchEvent('Model.afterDelete', ['entity' => $entity, 'options' => $options]);
     return $success;
 }