Skip to content

Commit

Permalink
Using transactions in all destructive methods in TreeBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 8, 2014
1 parent fa4adc1 commit 122c754
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -339,6 +339,19 @@ public function findChildren($query, $options) {
* false on error
*/
public function removeFromTree(Entity $node) {
return $this->_table->connection()->transactional(function() use ($node) {
return $this->_removeFromTree($node);
});
}

/**
* Helper function contianing the actual code for removeFromTree
*
* @param \Cake\ORM\Entity $node The node to remove from the tree
* @return \Cake\ORM\Entity|false the node after being removed from the tree or
* false on error
*/
protected function _removeFromTree($node) {
$config = $this->config();
$left = $node->get($config['left']);
$right = $node->get($config['right']);
Expand Down Expand Up @@ -382,6 +395,20 @@ public function removeFromTree(Entity $node) {
* @return \Cake\ORM\Entity|boolean $node The node after being moved or false on failure
*/
public function moveUp(Entity $node, $number = 1) {
return $this->_table->connection()->transactional(function() use ($node, $number) {
return $this->_moveUp($node, $number);
});
}

/**
* Helper function used with the actual code for moveUp
*
* @param \Cake\ORM\Entity $node The node to move
* @param integer|boolean $number How many places to move the node, or true to move to first position
* @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
* @return \Cake\ORM\Entity|boolean $node The node after being moved or false on failure
*/
protected function _moveUp($node, $number) {
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];

Expand Down Expand Up @@ -442,6 +469,20 @@ public function moveUp(Entity $node, $number = 1) {
* @return \Cake\ORM\Entity|boolean the entity after being moved or false on failure
*/
public function moveDown(Entity $node, $number = 1) {
return $this->_table->connection()->transactional(function() use ($node, $number) {
return $this->_moveDown($node, $number);
});
}

/**
* Helper function used with the actual code for moveDown
*
* @param \Cake\ORM\Entity $node The node to move
* @param integer|boolean $number How many places to move the node, or true to move to last position
* @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
* @return \Cake\ORM\Entity|boolean $node The node after being moved or false on failure
*/
protected function _moveDown($node, $number) {
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];

Expand Down

0 comments on commit 122c754

Please sign in to comment.