/** * Unbind associated relations based on the mode * * @param model class $model * @param string $mode Default is 'all'. Accepts hasMany, hasOne * belongsTo, hasAndBelongsToMany, and all * @return void */ public function unbindAll(Model $model, $mode = 'all') { $hasMany = array_keys($model->hasMany); $belongsTo = array_keys($model->belongsTo); $hasOne = array_keys($model->hasOne); $habtm = array_keys($model->hasAndBelongsToMany); switch ($mode) { case 'hasMany': $model->unbindModel(array('hasMany' => $hasMany)); break; case 'hasOne': $model->unbindModel(array('hasOne' => $hasOne)); break; case 'belongsTo': $model->unbindModel(array('belongsTo' => $belongsTo)); break; case 'hasAndBelongsToMany': $model->unbindModel(array('hasAndBelongsToMany' => $habtm)); break; case 'all': $model->unbindModel(array('hasMany' => $hasMany)); $model->unbindModel(array('hasOne' => $hasOne)); $model->unbindModel(array('belongsTo' => $belongsTo)); $model->unbindModel(array('hasAndBelongsToMany' => $habtm)); break; } }
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); }
public function getCustomFieldValues(Model $model, $options = array()) { $module = $this->settings[$model->alias]['module']; $valueModel = $module . 'CustomValue'; $conditions = array(); $key = $valueModel . '.' . Inflector::underscore($module . 'Id'); if (array_key_exists('id', $options)) { $conditions[$key] = $options['id']; } $model->unbindModel(array('belongsTo' => array('ModifiedUser', 'CreatedUser'))); $model->bindModel(array('hasMany' => array($valueModel => array('conditions' => $conditions)))); $valuesData = $model->find('all', array('order' => $model->alias . '.order')); $processedData = array(); foreach ($valuesData as $key => $value) { if (!empty($value[$valueModel])) { array_push($processedData, $value[$valueModel][0]); } else { array_push($processedData, array()); } } return $processedData; }
/** * 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 Model $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 * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::recover */ public 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->name, '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) { $lft = $count++; $rght = $count++; $Model->create(false); $Model->id = $array[$Model->alias][$Model->primaryKey]; $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(false); $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; }
/** * Trackableビヘイビアで必用な関連モデルが増えすぎるので除去する * * @param Model $Model Trackableを引きはがすモデル * @return void */ protected function _unloadTrackable(Model $Model) { $Model->Behaviors->unload('NetCommons.Trackable'); $Model->unbindModel(array('belongsTo' => array('TrackableCreator', 'TrackableUpdater')), false); }
/** * Clear all associations * * @param Model $Model * @param boolean $reset * @return void */ public function unbindAllModels(Model $Model, $reset = false) { $assocs = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'); $unbind = array(); foreach ($assocs as $assoc) { $unbind[$assoc] = array_keys($Model->{$assoc}); } $Model->unbindModel($unbind, $reset); }
/** * UnbindAll with Exceptions * Allows you to quickly unbindAll of a model's * associations with the exception of param 2. * * Usage: * $this->Model->unbindAll(); // unbinds ALL * $this->Model->unbindAll(array('hasMany' => array('Model2')) // unbind All except hasMany-Model2 * * @param Model $model * @param array $exceptions */ function unbindAll(&$model, $exceptions = array()) { $unbind = array(); foreach ($model->__associations as $type) { foreach ($model->{$type} as $assoc => $assocData) { // if the assoc is NOT in the exceptions list then // add it to the list of models to be unbound. if (@(!in_array($assoc, $exceptions[$type]))) { $unbind[$type][] = $assoc; } } } // if we actually have models to unbind if (count($unbind) > 0) { $model->unbindModel($unbind); } }
/** * Reset the temporary relations created during the beforeSave operation * * @param Model $model * @return void */ private function reset_relations(&$model) { foreach ($this->temporary_relations as $relation_type => $relations) { foreach ($relations as $relation_name) { $model->unbindModel(array($relation_type => array($relation_name))); } } }
/** * _unbind method * * unbind the Bitmask model from the model in question * * @param Model $Model * @return boolean */ protected function _unbind(Model $Model) { return $Model->unbindModel(array('hasOne' => array($this->getBitmaskedBitAlias($Model))), false); }
public function cleanup(Model $Model) { $Model->unbindModel(array('hasMany' => array('Comment')), false); }
/** * Before delete is called before any delete occurs on the attached model, but after the model's * beforeDelete is called. Returning false from a beforeDelete will abort the delete. * * We are unbinding the association model, so we can handle the delete ourselves * * @todo Might be a better way to do this with model associations * * @param Model $Model Model using this behavior * @param boolean $cascade If true records that depend on this record will also be deleted * @return mixed False if the operation should abort. Any other result will continue. */ public function beforeDelete(Model $Model, $cascade = true) { //unbinds the model, so we can handle the delete $Model->unbindModel(array('hasMany' => array('File'))); return true; }
/** * unbind the permission model from the model in question * * @param object $Model * @return boolean */ private function _unbind(Model $Model) { return $Model->unbindModel(array('hasOne' => array($this->getPermissionAlias($Model))), false); }