Exemple #1
0
 /**
  * Deletes/sets null the related objects matching $conditions.
  * The action which is taken depends on the dependency between source and targets and also on foreign key nullability
  *
  * @param array $foreignKey array of foreign key properties
  * @param \Cake\ORM\Table $target The associated table
  * @param array $conditions The conditions that specifies what are the objects to be unlinked
  * @param array $options list of options accepted by `Table::delete()`
  * @return bool success
  */
 protected function _unlink(array $foreignKey, Table $target, array $conditions = [], array $options = [])
 {
     $mustBeDependent = !$this->_foreignKeyAcceptsNull($target, $foreignKey) || $this->dependent();
     if ($mustBeDependent) {
         if ($this->_cascadeCallbacks) {
             $conditions = new QueryExpression($conditions);
             $conditions->traverse(function ($entry) use($target) {
                 if ($entry instanceof FieldInterface) {
                     $entry->setField($target->aliasField($entry->getField()));
                 }
             });
             $query = $this->find('all')->where($conditions);
             $ok = true;
             foreach ($query as $assoc) {
                 $ok = $ok && $target->delete($assoc, $options);
             }
             return $ok;
         }
         $target->deleteAll($conditions);
         return true;
     }
     $updateFields = array_fill_keys($foreignKey, null);
     $target->updateAll($updateFields, $conditions);
     return true;
 }