Skip to content

Commit

Permalink
Merge pull request cakephp#1585 from dereuromark/2.4-generatetreelist…
Browse files Browse the repository at this point in the history
…-scope

Bugfix: Add scope for generateTreeList

Fixes cakephp#4028
  • Loading branch information
markstory committed Sep 1, 2013
2 parents fec646b + eb71eee commit 1c080b2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Cake/Model/Behavior/TreeBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ public function generateTreeList(Model $Model, $conditions = null, $keyPath = nu
} else {
array_unshift($valuePath, '%s' . $valuePath[0], '{n}.tree_prefix');
}

$conditions = (array)$conditions;
if ($scope) {
$conditions[] = $scope;
}

$order = $Model->escapeField($left) . " asc";
$results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
$stack = array();
Expand Down
40 changes: 40 additions & 0 deletions lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,44 @@ public function testAliasesWithScopeInTwoTreeAssociations() {
));
$this->assertEquals($expected, $result);
}

/**
* testGenerateTreeListWithScope method
*
* @return void
*/
public function testGenerateTreeListWithScope() {
extract($this->settings);
$this->Tree = new $modelClass();
$this->Tree->order = null;
$this->Tree->initialize(2, 3);

$this->Tree->id = 1;
$this->Tree->saveField('flag', 1);
$this->Tree->id = 2;
$this->Tree->saveField('flag', 1);

$this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));

$result = $this->Tree->generateTreeList();
$expected = array(
1 => '1. Root',
2 => '_1.1'
);
$this->assertEquals($expected, $result);

// As string.
$this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));

$result = $this->Tree->generateTreeList();
$this->assertEquals($expected, $result);

// Merging conditions.
$result = $this->Tree->generateTreeList(array('FlagTree.id >' => 1));
$expected = array(
2 => '1.1'
);
$this->assertEquals($expected, $result);
}

}

0 comments on commit 1c080b2

Please sign in to comment.