Skip to content

Commit

Permalink
update refactoring of children() method
Browse files Browse the repository at this point in the history
- is now find('children')
- test cases updated
  • Loading branch information
quickapps authored and lorenzo committed Mar 29, 2014
1 parent c3d6199 commit bc65eff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
66 changes: 31 additions & 35 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -40,7 +40,10 @@ class TreeBehavior extends Behavior {
* @var array
*/
protected static $_defaultConfig = [
'implementedFinders' => ['path' => 'findPath'],
'implementedFinders' => [
'path' => 'findPath',
'children' => 'findChildren',
],
'parent' => 'parent_id',
'left' => 'lft',
'right' => 'rght',
Expand Down Expand Up @@ -100,57 +103,50 @@ public function childCount($id, $direct = false) {
/**
* Get the child nodes of the current model
*
* If the direct parameter is set to true, only the direct children are returned (based upon the parent_id field)
* Available options are:
*
* - direct: Whether to return only the direct, or all, children
* - order: SQL ORDER BY conditions (e.g. ['price' => 'DESC']) defaults to the tree order
*
* If the direct option is set to true, only the direct children are returned (based upon the parent_id field)
* If false is passed for the id parameter, top level, or all (depending on direct parameter appropriate) are counted.
*
* @param integer|string $id The ID of the record to read
* @param boolean $direct whether to return only the direct, or all, children
* @param string|array $fields Either a single string of a field name, or an array of field names
* @param array $order SQL ORDER BY conditions (e.g. ['price' => 'DESC']) defaults to the tree order
* @param integer $limit SQL LIMIT clause, for calculating items per page.
* @param integer $page Page number, for accessing paged data
* @return array Array of child nodes
* @param array $options Array of options as described above
* @return \Cake\ORM\Query
*/
public function children($id, $direct = false, $fields = [], $order = [], $limit = null, $page = 1) {
public function findChildren($query, $options) {
extract($this->config());
extract($options);
$primaryKey = $this->_table->primaryKey();
$direct = !isset($direct) ? false : $direct;
$order = !isset($order) ? [$left => 'ASC'] : $order;

if (!isset($for) || empty($for)) {
throw new \InvalidArgumentException("The 'for' key is required for find('children')");
}

if ($direct) {
return $this->_scope($this->_table->find())
->where([$parent => $id])
->all();
return $this->_scope($query)
->where([$parent => $for])
->order($order);
}

$node = $this->_scope($this->_table->find())
->select([$right, $left])
->where([$primaryKey => $id])
->where([$primaryKey => $for])
->first();

if (!$node) {
return false;
}

$order = !$order ? [$left => 'ASC'] : $order;
$query = $this->_scope($this->_table->find());

if ($fields) {
$query->select($fields);
}

$query->where([
"{$right} <" => $node->{$right},
"{$left} >" => $node->{$left}
]);

if ($limit) {
$query->limit($limit);
}

if ($page) {
$query->page($page);
return $query;
}

return $query->order($order)->all();
return $this->_scope($query)
->where([
"{$right} <" => $node->{$right},
"{$left} >" => $node->{$left}
])
->order($order);
}

/**
Expand Down
22 changes: 17 additions & 5 deletions tests/TestCase/Model/Behavior/TreeBehaviorTest.php
Expand Up @@ -125,26 +125,37 @@ public function testCallableScoping() {
*
* @return void
*/
public function testChildren() {
public function testFindChildren() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);

// root
$nodeIds = [];
foreach ($table->children(1) as $node) {
$nodes = $table->find('children', ['for' => 1])
->all();
foreach ($nodes as $node) {
$nodeIds[] = $node->id;
}
$this->assertEquals([2, 3, 4, 5], $nodeIds);

// unexisting node
$this->assertEquals(false, $table->children(500));
$query = $table->find('children', ['for' => 500]);
$this->assertEquals($query, $table->find('children', ['for' => 500]));

// leaf
$nodeIds = [];
foreach ($table->children(5) as $node) {
$nodes = $table->find('children', ['for' => 5])->all();
foreach ($nodes as $node) {
$nodeIds[] = $node->id;
}
$this->assertEquals(0, count($nodeIds));

// direct children
$nodes = $table->find('children', ['for' => 1, 'direct' => true])->all();
foreach ($nodes as $node) {
$nodeIds[] = $node->id;
}
$this->assertEquals([2, 3], $nodeIds);
}

/**
Expand All @@ -166,7 +177,8 @@ public function testMoveUp() {
// move inner node
$nodeIds = [];
$result = $table->moveUp(3, 1);
foreach ($table->children(1) as $node) {
$nodes = $table->find('children', ['for' => 1])->all();
foreach ($nodes as $node) {
$nodeIds[] = $node->id;
}
$this->assertEquals([3, 4, 5, 2], $nodeIds);
Expand Down

0 comments on commit bc65eff

Please sign in to comment.