function main() { if (empty($this->args)) { return $this->err('Usage: ./cake uuidize <table>'); } if ($this->args[0] == '?') { return $this->out('Usage: ./cake uuidize <table> [-force] [-reindex]'); } $options = array('force' => false, 'reindex' => false); foreach ($this->params as $key => $val) { foreach ($options as $name => $option) { if (isset($this->params[$name]) || isset($this->params['-' . $name]) || isset($this->params[$name[0]])) { $options[$name] = true; } } } foreach ($this->args as $table) { $name = Inflector::classify($table); $Model = new AppModel(array('name' => $name, 'table' => $table)); $records = $Model->find('all'); foreach ($records as $record) { $Model->updateAll(array('id' => '"' . String::uuid() . '"'), array('id' => $record[$name]['id'])); } } }
/** * 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 AppModel $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 * @access public * @link http://book.cakephp.org/view/1354/removeFromTree */ 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($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)); } }
/** * 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 AppModel $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 */ 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($parent => $node[$Model->primaryKey])); $Model->id = $id; $this->__sync($Model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1)); $this->__sync($Model, 2, '-', '> ' . $node[$right]); if ($delete) { $sub_tree = $Model->find('list', array('conditions' => array($left . ' >= ' . $node[$left], $right . ' <= ' . $node[$right]), 'fields' => array('id', 'parent_id'))); foreach ($sub_tree as $sChild => $sParent) { $Model->del($sChild); } return true; } else { /* * this could feasibly create a lot of new trees.. worst case * performance slightly worse than original */ $edge = $this->__getPartition($Model, $scope, $recursive); if ($node[$right] == $edge) { $edge = $edge - 2; } $Model->id = $id; return $Model->save(array($left => $edge, $right => $edge + 1, $parent => null), array('callbacks' => false)); } }
/** * 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 AppModel $model * @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 * @access public */ function removefromtree(&$model, $id = null, $delete = false) { if (empty($id)) { $id = $model->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' => -1))); if ($node[$right] == $node[$left] + 1) { if ($delete) { $model->delete(); } else { return false; } } 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' => -1))); } else { $parentNode[$right] = $node[$right] + 1; } $model->updateAll(array($parent => $node[$parent]), array($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) => null, $model->escapeField($right) => null, $model->escapeField($parent) => null), array($model->escapeField() => $id)); return $model->delete($id); } else { $edge = $this->__getMax($model, $scope, $right); if ($node[$right] == $edge) { $edge = $edge - 2; } $model->id = $id; return $model->save(array($left => $edge + 1, $right => $edge + 2, $parent => null)); } }
/** * 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. * * @since 1.2 * @param AppModel $model * @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 * @access public */ function remove_from_tree(&$model, $id = null, $delete = false) { if (empty($id)) { $id = $model->id; } extract($this->settings[$model->name]); list($node) = array_values($model->find(array($scope, $model->escapeField() => $id), array($model->primaryKey, $left, $right, $parent), null, -1)); if ($node[$right] == $node[$left] + 1) { return false; } elseif ($node[$parent]) { list($parentNode) = array_values($model->find(array($scope, $model->escapeField() => $node[$parent]), array($model->primaryKey, $left, $right), null, -1)); } else { $parentNode[$right] = $node[$right] + 1; } $model->updateAll(array($parent => $node[$parent]), array($parent => $node[$model->primaryKey])); $this->__sync($model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1), $scope); $this->__sync($model, 2, '-', '> ' . $node[$right], $scope); $model->id = $id; if ($delete) { // Bypass beforesave method $model->updateAll(array($left => null, $right => null, $parent => null), array($model->primaryKey => $id)); // delete the node return $model->delete($id); } else { $edge = $this->__get_max($model, $scope, $right); if ($node[$right] == $edge) { // The node being deleted, is the last top level node, adjust edge value excluding // the influence of the current node. $edge = $edge - 2; } $model->id = $id; return $model->save(array($left => $edge + 1, $right => $edge + 2, $parent => null)); } }
/** * This has the effect of moving all the higher items up one. * * @param AppModel $model * @param integer $position * @return boolean */ private function __decrementPositionsOnHigherItems($model, $position) { extract($this->settings[$model->alias]); return $model->updateAll(array($model->alias . '.' . $positionColumn => $model->alias . '.' . $positionColumn . '-1'), array($this->__scopeCondition($model), $model->alias . '.' . $positionColumn . ' <= ' => $position)); }