mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Enable use of Containable with TreeBehaviors
This patch allows Containable use with: - TreeBehavior::getParentNode() - TreeBehavior::children() - TreeBehavior::getPath()
This commit is contained in:
parent
1651e7742c
commit
45be270a7b
2 changed files with 70 additions and 6 deletions
|
@ -306,7 +306,9 @@ class TreeBehavior extends ModelBehavior {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::children
|
* @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) {
|
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)) {
|
if (is_array($id)) {
|
||||||
|
$options = $this->_getOptions($id);
|
||||||
extract(array_merge(array('id' => null), $id));
|
extract(array_merge(array('id' => null), $id));
|
||||||
}
|
}
|
||||||
$overrideRecursive = $recursive;
|
$overrideRecursive = $recursive;
|
||||||
|
@ -348,7 +350,10 @@ class TreeBehavior extends ModelBehavior {
|
||||||
$Model->escapeField($left) . ' >' => $result[0][$left]
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -426,7 +431,9 @@ class TreeBehavior extends ModelBehavior {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getParentNode
|
* @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) {
|
public function getParentNode(Model $Model, $id = null, $fields = null, $recursive = null) {
|
||||||
|
$options = array();
|
||||||
if (is_array($id)) {
|
if (is_array($id)) {
|
||||||
|
$options = $this->_getOptions($id);
|
||||||
extract(array_merge(array('id' => null), $id));
|
extract(array_merge(array('id' => null), $id));
|
||||||
}
|
}
|
||||||
$overrideRecursive = $recursive;
|
$overrideRecursive = $recursive;
|
||||||
|
@ -446,18 +453,32 @@ class TreeBehavior extends ModelBehavior {
|
||||||
|
|
||||||
if ($parentId) {
|
if ($parentId) {
|
||||||
$parentId = $parentId[$Model->alias][$parent];
|
$parentId = $parentId[$Model->alias][$parent];
|
||||||
$parent = $Model->find('first', array(
|
$options = array_merge(array(
|
||||||
'conditions' => array($Model->escapeField() => $parentId),
|
'conditions' => array($Model->escapeField() => $parentId),
|
||||||
'fields' => $fields,
|
'fields' => $fields,
|
||||||
'order' => false,
|
'order' => false,
|
||||||
'recursive' => $recursive
|
'recursive' => $recursive
|
||||||
));
|
), $options);
|
||||||
|
$parent = $Model->find('first', $options);
|
||||||
|
|
||||||
return $parent;
|
return $parent;
|
||||||
}
|
}
|
||||||
return false;
|
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
|
* Get the path to the given node
|
||||||
*
|
*
|
||||||
|
@ -469,9 +490,21 @@ class TreeBehavior extends ModelBehavior {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getPath
|
* @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) {
|
public function getPath(Model $Model, $id = null, $fields = null, $recursive = null) {
|
||||||
|
$options = array();
|
||||||
if (is_array($id)) {
|
if (is_array($id)) {
|
||||||
|
$options = $this->_getOptions($id);
|
||||||
extract(array_merge(array('id' => null), $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;
|
$overrideRecursive = $recursive;
|
||||||
if (empty($id)) {
|
if (empty($id)) {
|
||||||
$id = $Model->id;
|
$id = $Model->id;
|
||||||
|
@ -492,12 +525,17 @@ class TreeBehavior extends ModelBehavior {
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
$item = $result[0];
|
$item = $result[0];
|
||||||
$results = $Model->find('all', array(
|
$options = array_merge(array(
|
||||||
'conditions' => array($scope, $Model->escapeField($left) . ' <=' => $item[$left], $Model->escapeField($right) . ' >=' => $item[$right]),
|
'conditions' => array(
|
||||||
|
$scope,
|
||||||
|
$Model->escapeField($left) . ' <=' => $item[$left],
|
||||||
|
$Model->escapeField($right) . ' >=' => $item[$right],
|
||||||
|
),
|
||||||
'fields' => $fields,
|
'fields' => $fields,
|
||||||
'order' => array($Model->escapeField($left) => 'asc'),
|
'order' => array($Model->escapeField($left) => 'asc'),
|
||||||
'recursive' => $recursive
|
'recursive' => $recursive
|
||||||
));
|
), $options);
|
||||||
|
$results = $Model->find('all', $options);
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -239,6 +239,32 @@ class ModelIntegrationTest extends BaseModelTest {
|
||||||
$this->assertFalse(isset($TestModel->Behaviors->Tree));
|
$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
|
* testFindWithJoinsOption method
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue