Skip to content

Commit

Permalink
Implemented TreeBehavior::recover()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 29, 2014
1 parent c8d686f commit 9efddaa
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
34 changes: 34 additions & 0 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -269,6 +269,40 @@ public function moveDown($id, $number = 1) {
return true;
}

public function recover() {
$this->_table->connection()->transactional(function() {
$this->_recoverTree();
});
}

protected function _recoverTree($counter = 0, $parentId = null) {
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
$pk = (array)$this->_table->primaryKey();

$query = $this->_scope($this->_table->query())
->select($pk)
->where(function($exp) use ($parentId, $parent) {
return $parentId === null ? $exp->isNull($parent) : $exp->eq($parent, $parentId);
})
->order($pk)
->hydrate(false)
->bufferResults(false);

$leftCounter = $counter;
foreach ($query as $row) {
$counter++;
$counter = $this->_recoverTree($counter, $row[$pk[0]]);
}

$this->_table->updateAll(
[$left => $leftCounter, $right => $counter + 1],
[$pk[0] => $parentId]
);

return $counter + 1;
}

/**
* Get the maximum index value in the table.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/MenuLinkTreeFixture.php
Expand Up @@ -36,8 +36,8 @@ class MenuLinkTreeFixture extends TestFixture {
public $fields = array(
'id' => ['type' => 'integer'],
'menu' => ['type' => 'string', 'null' => false],
'lft' => ['type' => 'integer', 'null' => false],
'rght' => ['type' => 'integer', 'null' => false],
'lft' => ['type' => 'integer'],
'rght' => ['type' => 'integer'],
'parent_id' => 'integer',
'url' => ['type' => 'string', 'null' => false],
'title' => ['type' => 'string', 'null' => false],
Expand Down
7 changes: 4 additions & 3 deletions tests/Fixture/NumberTreeFixture.php
Expand Up @@ -37,8 +37,8 @@ class NumberTreeFixture extends TestFixture {
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'null' => false],
'parent_id' => 'integer',
'lft' => ['type' => 'integer', 'null' => false],
'rght' => ['type' => 'integer', 'null' => false],
'lft' => ['type' => 'integer'],
'rght' => ['type' => 'integer'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
);

Expand All @@ -55,6 +55,7 @@ class NumberTreeFixture extends TestFixture {
* - flash:8
* - cd:9
* - radios:10
* - alien ware: 11
*
* @var array
*/
Expand Down Expand Up @@ -134,7 +135,7 @@ class NumberTreeFixture extends TestFixture {
'name' => 'alien hardware',
'parent_id' => null,
'lft' => '21',
'rght' => '21'
'rght' => '22'
)
);

Expand Down
40 changes: 40 additions & 0 deletions tests/TestCase/Model/Behavior/TreeBehaviorTest.php
Expand Up @@ -251,4 +251,44 @@ public function testMoveDownException() {
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$table->moveDown(500, 1);
}

/**
* Tests the recover function
*
* @return void
*/
public function testRecover() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$table->updateAll(['lft' => null, 'rght' => null], []);
$table->recover();
$result = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}

/**
* Tests the recover function with a custom scope
*
* @return void
*/
public function testRecoverScoped() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$expected = $table->find()
->where(['menu' => 'main-menu'])
->order('lft')
->hydrate(false)
->toArray();

$table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
$table->recover();
$result = $table->find()
->where(['menu' => 'main-menu'])
->order('lft')
->hydrate(false)
->toArray();
$this->assertEquals($expected, $result);
}

}

0 comments on commit 9efddaa

Please sign in to comment.