public function afterSave(Model $model, $created, $options = array()) { if ($model->data[$model->alias]['main'] != '0') { // if main was set as 1.. set the rest to 0 $model->updateAll(array($model->alias . '.main' => "0"), array($model->alias . '.contact_type_id = ' => $model->data[$model->alias]['contact_type_id'], $model->alias . '.id <> ' => $model->id, $model->alias . '.security_user_id = ' => $model->data[$model->alias]['security_user_id'])); } return parent::afterSave($model, $created, $options); }
public function updateAll($fields, $conditions = true) { $this->useDbConfig = 'master'; $status = parent::updateAll($fields, $conditions); if (!isset($_SERVER['FORCEMASTER'])) { $this->useDbConfig = 'default'; } return $status; }
function updateAll($fields, $conditions = true, $recursive = null) { if (!isset($recursive)) { $recursive = $this->recursive; } if ($recursive == -1) { $this->unbindModel(array('belongsTo' => array_keys($this->belongsTo), 'hasOne' => array_keys($this->hasOne)), true); } return parent::updateAll($fields, $conditions); }
/** * {@inheritDoc} */ public function updateAll($fields, $conditions = true) { $e = null; if ($this->_stopwatch) { $e = $this->_getStopwatchEvent('updateAll'); } $result = parent::updateAll($fields, $conditions); if ($e !== null) { $e->stop(); } return $result; }
/** * Update bbs_article_child_count * * @param object $model instance of model * @param int $rootId RootId for root BbsArticle * @param int $languageId languages.id * @return mixed On success Model::$data if its not empty or true, false on failure * @throws InternalErrorException */ public function updateBbsArticleChildCount(Model $model, $rootId, $languageId) { $rootId = (int) $rootId; $conditions = array('BbsArticleTree.root_id' => $rootId, 'BbsArticle.language_id' => $languageId, 'BbsArticle.is_active' => true); $count = $model->find('count', array('recursive' => 0, 'conditions' => $conditions)); if ($count === false) { throw new InternalErrorException(__d('net_commons', 'Internal Server Error')); } $update = array('BbsArticleTree.bbs_article_child_count' => $count); $conditions = array('BbsArticleTree.id' => $rootId); if (!$model->updateAll($update, $conditions)) { throw new InternalErrorException(__d('net_commons', 'Internal Server Error')); } }
/** * Updates multiple model records based on a set of conditions. * * call afterSave() callback after successful update. * * @param array $fields Set of fields and values, indexed by fields. * Fields are treated as SQL snippets, to insert literal values manually escape your data. * @param mixed $conditions Conditions to match, true for all records * @return boolean True on success, false on failure * @access public */ function updateAll($fields, $conditions = true) { $output = parent::updateAll($fields, $conditions); if ($output) { $created = false; $options = array(); $this->Behaviors->trigger($this, 'afterSave', array($created, $options)); $this->afterSave($created); $this->_clearCache(); $this->id = false; return true; } return false; }
public function beforeSave(Model $model, $options = array()) { $schema = $model->schema(); $data = current($model->data); if (isset($schema['default']) && isset($data['default'])) { // because they are still in their own tables.. // $conditionId = $model->getConditionId(); $default = $data['default']; if ($default == 1) { $model->updateAll(array($model->alias . '.default' => 0)); } } return true; }
/** * Updates a database record with the necessary extra data * * @param Model $model Model instance * @param array $data array containing data to be saved to the record * @return void */ protected function _updateRecord(Model $model, $data) { if (!empty($data[$model->alias])) { $model->updateAll($data[$model->alias], array($model->alias . '.' . $model->primaryKey => $model->id)); } }
/** * Increment or decrement the comment count cache on the associated model * * @param Object $model Model to change count of * @param mixed $id The id to change count of * @param string $direction 'up' or 'down' * @return null */ public function changeCommentCount(Model $model, $id = null, $direction = 'up') { if ($model->hasField('comments')) { if ($direction == 'up') { $direction = '+ 1'; } elseif ($direction == 'down') { $direction = '- 1'; } else { $direction = null; } $model->id = $id; if (!is_null($direction) && $model->exists(true)) { return $model->updateAll(array($model->alias . '.comments' => $model->alias . '.comments ' . $direction), array($model->alias . '.id' => $id)); } } return false; }
/** * Moves all lower items one position down * * @param object AppModel * @param integer * @return boolean */ private function __incrementPositionsOnLowerItems(Model $model, $position) { extract($this->settings[$model->alias]); $positionColumn = $this->settings[$model->alias]['positionColumn']; return $model->updateAll(array($model->alias . '.' . $positionColumn => $model->alias . '.' . $positionColumn . '+1'), array($this->__scopeCondition($model), $model->alias . '.' . $positionColumn . ' >= ' => $position)); }
/** * Updates multiple model records based on a set of conditions. * * @param array $fields Set of fields and values, indexed by fields. * Fields are treated as SQL snippets, to insert literal values manually escape your data. * @param mixed $conditions Conditions to match, true for all records * @return boolean True on success, false on failure * @link http://book.cakephp.org/view/75/Saving-Your-Data */ public function updateAll($fields, $conditions = true) { $result = parent::updateAll($fields, $conditions); if ($result) { if ($this->Behaviors->attached('BcCache') && $this->Behaviors->enabled('BcCache')) { $this->delCache($this); } } return $result; }
/** * Updates multiple model records based on a set of conditions. * * @param array $fields Set of fields and values, indexed by fields. * Fields are treated as SQL snippets, to insert literal values manually escape your data. * @param mixed $conditions Conditions to match, true for all records * @return boolean True on success, false on failure * @access public * @link http://book.cakephp.org/view/75/Saving-Your-Data */ function updateAll($fields, $conditions = true) { $result = parent::updateAll($fields, $conditions); if ($result) { if ($this->Behaviors->attached('Cache') && $this->Behaviors->enabled('Cache')) { if ($this->cacheEnabled()) { $this->cacheDelete($this); } } } return $result; }
/** * Sets the weight for new items so they end up at end * * @todo add new model with weight. clean up after * @param Model $Model */ public function beforeSave(&$Model) { // Check if weight id is set. If not add to end, if set update all // rows from ID and up if (!isset($Model->data[$Model->alias][$Model->primaryKey]) || isset($Model->data[$Model->alias][$this->settings[$Model->alias]['field']]) && !is_numeric($Model->data[$Model->alias][$this->settings[$Model->alias]['field']]) || $this->settings[$Model->alias]['foreign_key'] && isset($Model->data[$Model->alias][$this->settings[$Model->alias]['foreign_key']]) && $Model->data[$Model->alias][$this->settings[$Model->alias]['foreign_key']] != $Model->field($this->settings[$Model->alias]['foreign_key'])) { $fk = null; if ($this->settings[$Model->alias]['foreign_key']) { if (!isset($Model->data[$Model->alias][$this->settings[$Model->alias]['foreign_key']])) { trigger_error('OrderedBehavior : New rows must be saved with foreign key field present.', E_USER_WARNING); return false; } $fk = $Model->data[$Model->alias][$this->settings[$Model->alias]['foreign_key']]; // foreign_key changed if ($fk != $Model->field($this->settings[$Model->alias]['foreign_key'])) { // move down all items with old foreign_key and weight > weight of changed item $conditions = array($Model->alias . '.' . $this->settings[$Model->alias]['foreign_key'] => $Model->field($this->settings[$Model->alias]['foreign_key']), $Model->alias . '.' . $this->settings[$Model->alias]['field'] . ' >' => $Model->field($this->settings[$Model->alias]['field'])); $Model->updateAll(array($this->settings[$Model->alias]['field'] => $Model->alias . '.' . $this->settings[$Model->alias]['field'] . ' - 1'), $conditions); } } $Model->data[$Model->alias][$this->settings[$Model->alias]['field']] = $this->_newWeight($Model, $fk); } return true; }
/** * Remove the current node from the tree, and reparent all children up one level. * * If the parameter delete is false, the node will become a new top level node. Otherwise the node will be deleted * after the children are reparented. * * @param Model $Model Model instance * @param mixed $id The ID of the record to remove * @param boolean $delete whether to delete the node after reparenting children (if any) * @return boolean true on success, false on failure * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::removeFromTree */ public function removeFromTree($Model, $id = null, $delete = false) { if (is_array($id)) { extract(array_merge(array('id' => null), $id)); } extract($this->settings[$Model->alias]); list($node) = array_values($Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $id), 'fields' => array($Model->primaryKey, $left, $right, $parent), 'recursive' => $recursive))); if ($node[$right] == $node[$left] + 1) { if ($delete) { return $Model->delete($id); } else { $Model->id = $id; return $Model->saveField($parent, null); } } elseif ($node[$parent]) { list($parentNode) = array_values($Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $node[$parent]), 'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive))); } else { $parentNode[$right] = $node[$right] + 1; } $db = ConnectionManager::getDataSource($Model->useDbConfig); $Model->updateAll(array($parent => $db->value($node[$parent], $parent)), array($Model->escapeField($parent) => $node[$Model->primaryKey])); $this->_sync($Model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1)); $this->_sync($Model, 2, '-', '> ' . $node[$right]); $Model->id = $id; if ($delete) { $Model->updateAll(array($Model->escapeField($left) => 0, $Model->escapeField($right) => 0, $Model->escapeField($parent) => null), array($Model->escapeField() => $id)); return $Model->delete($id); } else { $edge = $this->_getMax($Model, $scope, $right, $recursive); if ($node[$right] == $edge) { $edge = $edge - 2; } $Model->id = $id; return $Model->save(array($left => $edge + 1, $right => $edge + 2, $parent => null), array('callbacks' => false)); } }
/** * _recoverByParentId * * Recursive helper function used by recover * * @param Model $Model Model instance. * @param int $counter Counter * @param int|string|null $parentId Parent record Id * @return int counter */ protected function _recoverByParentId(Model $Model, $counter = 1, $parentId = null) { $params = array('conditions' => array($this->settings[$Model->alias]['parent'] => $parentId), 'fields' => array($Model->primaryKey), 'page' => 1, 'limit' => 100, 'order' => array($Model->primaryKey)); $scope = $this->settings[$Model->alias]['scope']; if ($scope && ($scope !== '1 = 1' && $scope !== true)) { $params['conditions'][] = $scope; } $children = $Model->find('all', $params); $hasChildren = (bool) $children; if ($parentId !== null) { if ($hasChildren) { $Model->updateAll(array($this->settings[$Model->alias]['left'] => $counter), array($Model->escapeField() => $parentId)); $counter++; } else { $Model->updateAll(array($this->settings[$Model->alias]['left'] => $counter, $this->settings[$Model->alias]['right'] => $counter + 1), array($Model->escapeField() => $parentId)); $counter += 2; } } while ($children) { foreach ($children as $row) { $counter = $this->_recoverByParentId($Model, $counter, $row[$Model->alias][$Model->primaryKey]); } if (count($children) !== $params['limit']) { break; } $params['page']++; $children = $Model->find('all', $params); } if ($parentId !== null && $hasChildren) { $Model->updateAll(array($this->settings[$Model->alias]['right'] => $counter), array($Model->escapeField() => $parentId)); $counter++; } return $counter; }
public function updateAll($fields, $conditions = true) { $this->useMasterDb(); return parent::updateAll($fields, $conditions); }
/** * Internal helper method to save status fields * * @param Model $model Model object * @param array $ids Array of IDs * @param string $field Field to update * @param mixed $status Value to update * @return boolean True on success, false on failure */ protected function _saveStatus(Model $model, $ids, $field, $status) { return $model->updateAll(array($model->escapeField($field) => $status), array($model->escapeField() => $ids)); }
/** * Mark model instance(s) selected with a condition as 'read' * * @param Model $Model - Model * @param array $conditions - Array of conditions * @param bool $bool - True for 'read', false for 'unread' * @return bool True if success, false otherwise **/ protected function _markAllAs(Model $Model, $conditions = true, $bool = true) { return $Model->updateAll(array($Model->alias . '.' . $this->__settings[$Model->alias]['field'] => $bool), $conditions); }
public function afterDelete(Model $Model) { if ($Model->data) { // What was the weight of the deleted model? $oldWeight = $Model->data[$Model->alias][$this->settings[$Model->alias]['field']]; // update the weight of all models of higher weight by $action = array($this->settings[$Model->alias]['field'] => $Model->alias . '.' . $this->settings[$Model->alias]['field'] . ' - 1'); $conditions = array($Model->alias . '.' . $this->settings[$Model->alias]['field'] . ' >' => $oldWeight); if ($this->settings[$Model->alias]['foreign_key']) { $conditions[$Model->alias . '.' . $this->settings[$Model->alias]['foreign_key']] = $Model->data[$Model->alias][$this->settings[$Model->alias]['foreign_key']]; } // decreasing them by 1 return $Model->updateAll($action, $conditions); } return true; }
function updateAll($fields, $conditions = true) { $this->updateCounter(); return parent::updateAll($fields, $conditions); }
/** * Update the current shchema version in the database * * @access protected * @param mixed $sVersion The new schema version. Can be either an integer ( 1, 2, 3 ) or an expression ( version + 1 ) * @return void */ function _updateVersion($sVersion) { App::import('model'); $oTemp_model = new Model(false, 'schema_info'); $oTemp_model->updateAll(array('version' => $sVersion)); $this->_getMigrationVersion(); }
/** * ユーザの登録処理 * * @param Model $model ビヘイビア呼び出し元モデル * @param int $userId ユーザID * @return mixed On success Model::$data, false on failure * @throws InternalErrorException */ public function updateLoginTime(Model $model, $userId) { //トランザクションBegin $model->begin(); try { $update = array('User.previous_login' => 'User.last_login', 'User.last_login' => '\'' . date('Y-m-d H:i:s') . '\''); $conditions = array('User.id' => (int) $userId); if (!$model->updateAll($update, $conditions)) { throw new InternalErrorException(__d('net_commons', 'Internal Server Error')); } $model->commit(); } catch (Exception $ex) { $model->rollback($ex); } return true; }