Skip to content

Commit

Permalink
Add recoverSort to TreeBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Berry Goudswaard committed Sep 16, 2015
1 parent 6bc0893 commit e42b536
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/ORM/Behavior/TreeBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class TreeBehavior extends Behavior
'left' => 'lft',
'right' => 'rght',
'scope' => null,
'level' => null
'level' => null,
'recoverOrder' => null
];

/**
Expand Down Expand Up @@ -774,11 +775,12 @@ protected function _recoverTree($counter = 0, $parentId = null, $level = -1)
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
$primaryKey = $this->_getPrimaryKey();
$aliasedPrimaryKey = $this->_table->aliasField($primaryKey);
$order = $config['recoverOrder'] ? $config['recoverOrder'] : $aliasedPrimaryKey;

$query = $this->_scope($this->_table->query())
->select([$aliasedPrimaryKey])
->where([$this->_table->aliasField($parent) . ' IS' => $parentId])
->order([$aliasedPrimaryKey])
->order($order)
->hydrate(false);

$leftCounter = $counter;
Expand Down
32 changes: 25 additions & 7 deletions tests/TestCase/ORM/Behavior/TreeBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,13 +696,6 @@ 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();

Expand Down Expand Up @@ -735,6 +728,31 @@ public function testRecoverScoped()
$this->assertMpttValues($expected, $table);
}

/**
* Test recover function with a custom order clause
*
* @return void
*/
public function testRecoverWithCustomOrder()
{
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu'], 'recoverOrder' => ['MenuLinkTrees.title' => 'desc']]);
$table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
$table->recover();

$expected = [
' 1: 2 - 8:Link 8',
' 3: 6 - 6:Link 6',
'_ 4: 5 - 7:Link 7',
' 7:16 - 1:Link 1',
'_ 8:13 - 3:Link 3',
'__ 9:12 - 4:Link 4',
'___10:11 - 5:Link 5',
'_14:15 - 2:Link 2'
];
$this->assertMpttValues($expected, $table);
}

/**
* Tests adding a new orphan node
*
Expand Down

0 comments on commit e42b536

Please sign in to comment.