Skip to content

Commit

Permalink
Enable use of Containable with TreeBehaviors
Browse files Browse the repository at this point in the history
This patch allows Containable use with:

- TreeBehavior::getParentNode()
- TreeBehavior::children()
- TreeBehavior::getPath()
  • Loading branch information
rchavik committed Dec 20, 2014
1 parent 1651e77 commit 45be270
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
50 changes: 44 additions & 6 deletions lib/Cake/Model/Behavior/TreeBehavior.php
Expand Up @@ -306,7 +306,9 @@ public function childCount(Model $Model, $id = null, $direct = false) {
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::children
*/
public function children(Model $Model, $id = null, $direct = false, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {
$options = array();
if (is_array($id)) {
$options = $this->_getOptions($id);
extract(array_merge(array('id' => null), $id));
}
$overrideRecursive = $recursive;
Expand Down Expand Up @@ -348,7 +350,10 @@ public function children(Model $Model, $id = null, $direct = false, $fields = nu
$Model->escapeField($left) . ' >' => $result[0][$left]
);
}
return $Model->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
$options = array_merge(compact(
'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
), $options);
return $Model->find('all', $options);
}

/**
Expand Down Expand Up @@ -426,7 +431,9 @@ public function generateTreeList(Model $Model, $conditions = null, $keyPath = nu
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getParentNode
*/
public function getParentNode(Model $Model, $id = null, $fields = null, $recursive = null) {
$options = array();
if (is_array($id)) {
$options = $this->_getOptions($id);
extract(array_merge(array('id' => null), $id));
}
$overrideRecursive = $recursive;
Expand All @@ -446,18 +453,32 @@ public function getParentNode(Model $Model, $id = null, $fields = null, $recursi

if ($parentId) {
$parentId = $parentId[$Model->alias][$parent];
$parent = $Model->find('first', array(
$options = array_merge(array(
'conditions' => array($Model->escapeField() => $parentId),
'fields' => $fields,
'order' => false,
'recursive' => $recursive
));
), $options);
$parent = $Model->find('first', $options);

return $parent;
}
return false;
}

/**
* Convenience method to create default find() options from $arg when it is an
* associative array.
*
* @param array $arg Array
* @return array Options array
*/
protected function _getOptions($arg) {
return count(array_filter(array_keys($arg), 'is_string') > 0) ?
$arg :
array();
}

/**
* Get the path to the given node
*
Expand All @@ -469,9 +490,21 @@ public function getParentNode(Model $Model, $id = null, $fields = null, $recursi
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getPath
*/
public function getPath(Model $Model, $id = null, $fields = null, $recursive = null) {
$options = array();
if (is_array($id)) {
$options = $this->_getOptions($id);
extract(array_merge(array('id' => null), $id));
}

if (!empty($options)) {
$fields = null;
if (!empty($options['fields'])) {
$fields = $options['fields'];
}
if (!empty($options['recursive'])) {
$recursive = $options['recursive'];
}
}
$overrideRecursive = $recursive;
if (empty($id)) {
$id = $Model->id;
Expand All @@ -492,12 +525,17 @@ public function getPath(Model $Model, $id = null, $fields = null, $recursive = n
return array();
}
$item = $result[0];
$results = $Model->find('all', array(
'conditions' => array($scope, $Model->escapeField($left) . ' <=' => $item[$left], $Model->escapeField($right) . ' >=' => $item[$right]),
$options = array_merge(array(
'conditions' => array(
$scope,
$Model->escapeField($left) . ' <=' => $item[$left],
$Model->escapeField($right) . ' >=' => $item[$right],
),
'fields' => $fields,
'order' => array($Model->escapeField($left) => 'asc'),
'recursive' => $recursive
));
), $options);
$results = $Model->find('all', $options);
return $results;
}

Expand Down
26 changes: 26 additions & 0 deletions lib/Cake/Test/Case/Model/ModelIntegrationTest.php
Expand Up @@ -239,6 +239,32 @@ public function testDynamicBehaviorAttachment() {
$this->assertFalse(isset($TestModel->Behaviors->Tree));
}

/**
* testTreeWithContainable method
*
* @return void
*/
public function testTreeWithContainable() {
$this->loadFixtures('Ad', 'Campaign');
$TestModel = new Ad();
$TestModel->Behaviors->load('Tree');
$TestModel->Behaviors->load('Containable');

$node = $TestModel->findById(2);
$node['Ad']['parent_id'] = 1;
$TestModel->save($node);

$result = $TestModel->getParentNode(array('id' => 2, 'contain' => 'Campaign'));
$this->assertTrue(array_key_exists('Campaign', $result));

$result = $TestModel->children(array('id' => 1, 'contain' => 'Campaign'));
$this->assertTrue(array_key_exists('Campaign', $result[0]));

$result = $TestModel->getPath(array('id' => 2, 'contain' => 'Campaign'));
$this->assertTrue(array_key_exists('Campaign', $result[0]));
$this->assertTrue(array_key_exists('Campaign', $result[1]));
}

/**
* testFindWithJoinsOption method
*
Expand Down

0 comments on commit 45be270

Please sign in to comment.