/** * Unbind the all model mapping */ public function unbindModelAll() { $unbind = array(); foreach ($this->belongsTo as $model => $info) { $unbind['belongsTo'][] = $model; } foreach ($this->hasOne as $model => $info) { $unbind['hasOne'][] = $model; } foreach ($this->hasMany as $model => $info) { $unbind['hasMany'][] = $model; } foreach ($this->hasAndBelongsToMany as $model => $info) { $unbind['hasAndBelongsToMany'][] = $model; } parent::unbindModel($unbind); }
/** * Recover a corrupted tree * * The mode parameter is used to specify the source of info that is valid/correct. The opposite source of data * will be populated based upon that source of info. E.g. if the MPTT fields are corrupt or empty, with the $mode * 'parent' the values of the parent_id field will be used to populate the left and right fields. The missingParentAction * parameter only applies to "parent" mode and determines what to do if the parent field contains an id that is not present. * * @todo Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB. * @param AppModel $Model Model instance * @param string $mode parent or tree * @param mixed $missingParentAction 'return' to do nothing and return, 'delete' to * delete, or the id of the parent to set as the parent_id * @return boolean true on success, false on failure * @access public * @link http://book.cakephp.org/view/1628/Recover */ function recover(&$Model, $mode = 'parent', $missingParentAction = null) { if (is_array($mode)) { extract(array_merge(array('mode' => 'parent'), $mode)); } extract($this->settings[$Model->alias]); $Model->recursive = $recursive; if ($mode == 'parent') { $Model->bindModel(array('belongsTo' => array('VerifyParent' => array('className' => $Model->alias, 'foreignKey' => $parent, 'fields' => array($Model->primaryKey, $left, $right, $parent))))); $missingParents = $Model->find('list', array('recursive' => 0, 'conditions' => array($scope, array('NOT' => array($Model->escapeField($parent) => null), $Model->VerifyParent->escapeField() => null)))); $Model->unbindModel(array('belongsTo' => array('VerifyParent'))); if ($missingParents) { if ($missingParentAction == 'return') { foreach ($missingParents as $id => $display) { $this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')'; } return false; } elseif ($missingParentAction == 'delete') { $Model->deleteAll(array($Model->primaryKey => array_flip($missingParents))); } else { $Model->updateAll(array($parent => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents))); } } $count = 1; foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey), 'order' => $left)) as $array) { $Model->id = $array[$Model->alias][$Model->primaryKey]; $lft = $count++; $rght = $count++; $Model->save(array($left => $lft, $right => $rght), array('callbacks' => false)); } foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) { $Model->create(); $Model->id = $array[$Model->alias][$Model->primaryKey]; $this->_setParent($Model, $array[$Model->alias][$parent]); } } else { $db =& ConnectionManager::getDataSource($Model->useDbConfig); foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) { $path = $this->getpath($Model, $array[$Model->alias][$Model->primaryKey]); if ($path == null || count($path) < 2) { $parentId = null; } else { $parentId = $path[count($path) - 2][$Model->alias][$Model->primaryKey]; } $Model->updateAll(array($parent => $db->value($parentId, $parent)), array($Model->escapeField() => $array[$Model->alias][$Model->primaryKey])); } } return true; }