Permalink
Browse files

stub changes to have scoped trees have independence

they should have had individual lft/rght values since the beginning

'global' functions not updated - meaning recover and verify will break
with these changes ( or report false results)
  • Loading branch information...
1 parent 9089bba commit 5bb7d73eaa544d24705fc958b12d0e0b2fdb77c5 @AD7six AD7six committed Jan 27, 2012
@@ -43,8 +43,14 @@ class TreeBehavior extends ModelBehavior {
* @var array
*/
protected $_defaults = array(
- 'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght',
- 'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false, 'recursive' => -1
+ 'parent' => 'parent_id',
+ 'left' => 'lft',
+ 'right' => 'rght',
+ 'scope' => '1 = 1',
+ 'scopeField' => false,
+ 'type' => 'nested',
+ '__parentChange' => false,
+ 'recursive' => -1
);
/**
@@ -65,6 +71,7 @@ public function setup($Model, $config = array()) {
$data = $Model->getAssociated($settings['scope']);
$parent = $Model->{$settings['scope']};
$settings['scope'] = $Model->alias . '.' . $data['foreignKey'] . ' = ' . $parent->alias . '.' . $parent->primaryKey;
+ $settings['scopeField'] = $data['foreignKey'];
$settings['recursive'] = 0;
}
$this->settings[$Model->alias] = $settings;
@@ -117,6 +124,7 @@ public function beforeFind($Model, $query) {
*/
public function beforeDelete($Model, $cascade = true) {
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
list($name, $data) = array($Model->alias, $Model->read());
$data = $data[$name];
@@ -149,6 +157,7 @@ public function beforeDelete($Model, $cascade = true) {
*/
public function beforeSave($Model) {
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
$this->_addToWhitelist($Model, array($left, $right));
if (!$Model->id) {
@@ -227,6 +236,7 @@ public function childCount($Model, $id = null, $direct = false) {
$id = null;
}
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
if ($direct) {
return $Model->find('count', array('conditions' => array($scope, $Model->escapeField($parent) => $id)));
@@ -276,6 +286,7 @@ public function children($Model, $id = null, $direct = false, $fields = null, $o
}
$name = $Model->alias;
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
if (!is_null($overrideRecursive)) {
$recursive = $overrideRecursive;
@@ -323,6 +334,7 @@ public function children($Model, $id = null, $direct = false, $fields = null, $o
public function generateTreeList($Model, $conditions = null, $keyPath = null, $valuePath = null, $spacer = '_', $recursive = null) {
$overrideRecursive = $recursive;
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
if (!is_null($overrideRecursive)) {
$recursive = $overrideRecursive;
}
@@ -385,6 +397,7 @@ public function getParentNode($Model, $id = null, $fields = null, $recursive = n
$id = $Model->id;
}
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
if (!is_null($overrideRecursive)) {
$recursive = $overrideRecursive;
}
@@ -418,6 +431,7 @@ public function getPath($Model, $id = null, $fields = null, $recursive = null) {
$id = $Model->id;
}
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
if (!is_null($overrideRecursive)) {
$recursive = $overrideRecursive;
}
@@ -455,8 +469,12 @@ public function moveDown($Model, $id = null, $number = 1) {
}
if (empty ($id)) {
$id = $Model->id;
+ } else {
+ $Model->id = $id;
}
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
+
list($node) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $id),
'fields' => array($Model->primaryKey, $left, $right, $parent), 'recursive' => $recursive
@@ -513,8 +531,12 @@ public function moveUp($Model, $id = null, $number = 1) {
}
if (empty ($id)) {
$id = $Model->id;
+ } else {
+ $Model->id = $id;
}
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
+
list($node) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $id),
'fields' => array($Model->primaryKey, $left, $right, $parent), 'recursive' => $recursive
@@ -691,6 +713,7 @@ public function removeFromTree($Model, $id = null, $delete = false) {
extract (array_merge(array('id' => null), $id));
}
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
list($node) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $id),
@@ -831,6 +854,7 @@ public function verify($Model) {
*/
protected function _setParent($Model, $parentId = null, $created = false) {
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
list($node) = array_values($Model->find('first', array(
'conditions' => array($scope, $Model->escapeField() => $Model->id),
'fields' => array($Model->primaryKey, $parent, $left, $right),
@@ -907,10 +931,14 @@ protected function _getMax($Model, $scope, $right, $recursive = -1, $created = f
if (is_string($scope)) {
$scope .= " AND {$Model->alias}.{$Model->primaryKey} <> ";
$scope .= $db->value($Model->id, $Model->getColumnType($Model->primaryKey));
+ $scope = array($scope);
} else {
$scope['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
}
}
+
+ $scope = $this->_addScopeField($Model, $scope);
+
$name = $Model->alias . '.' . $right;
list($edge) = array_values($Model->find('first', array(
'conditions' => $scope,
@@ -940,6 +968,29 @@ protected function _getMin($Model, $scope, $left, $recursive = -1) {
return (empty($edge[$left])) ? 0 : $edge[$left];
}
+ protected function _addScopeField($Model, $scope) {
+ if (empty($this->settings[$Model->alias]['scopeField'])) {
+ return $scope;
+ }
+ if (!is_array($scope)) {
+ $scope = array($scope);
+ }
+ if ($Model->data) {
+ if (!empty($Model->data[$Model->alias][$this->settings[$Model->alias]['scopeField']])) {
+ $value = $Model->data[$Model->alias][$this->settings[$Model->alias]['scopeField']];
+ } else {
+ $value = $Model->field($this->settings[$Model->alias]['scopeField']);
+ }
+ } elseif ($Model->id) {
+ $value = $Model->field($this->settings[$Model->alias]['scopeField']);
+ } else {
+ $value = null;
+ }
+ $scope[][$Model->alias . '.' . $this->settings[$Model->alias]['scopeField']] = $value;
+
+ return $scope;
+ }
+
/**
* Table sync method.
*
@@ -956,6 +1007,7 @@ protected function _getMin($Model, $scope, $left, $recursive = -1) {
protected function _sync($Model, $shift, $dir = '+', $conditions = array(), $created = false, $field = 'both') {
$ModelRecursive = $Model->recursive;
extract($this->settings[$Model->alias]);
+ $scope = $this->_addScopeField($Model, $scope);
$Model->recursive = $recursive;
if ($field == 'both') {
@@ -136,12 +136,13 @@ public function testArrayScope() {
public function testMoveUpWithScope() {
$this->Ad = new Ad();
$this->Ad->Behaviors->attach('Tree', array('scope' => 'Campaign'));
- $this->Ad->moveUp(6);
+ $this->Ad->id = 6;
+ $this->Ad->moveUp();
$this->Ad->id = 4;
$result = $this->Ad->children();
- $this->assertEquals(Set::extract('/Ad/id', $result), array(6, 5));
- $this->assertEquals(Set::extract('/Campaign/id', $result), array(2, 2));
+ $this->assertEquals(array(6, 5), Set::extract('/Ad/id', $result));
+ $this->assertEquals(array(2, 2), Set::extract('/Campaign/id', $result));
}
/**
@@ -313,4 +314,18 @@ public function testAliasesWithScopeInTwoTreeAssociations() {
));
$this->assertEquals($expected, $result);
}
+
+ public function testNotOneBigTree() {
+ $this->Ad = new Ad();
+ $this->Ad->Behaviors->attach('Tree', array('scope' => 'Campaign'));
+ $this->Ad->deleteAll(true);
+
+ $expected = array(1, 2);
+ $return = $this->Ad->save(array('campaign_id' => 1));
+ $this->assertEquals($expected, array($return['Ad']['lft'], $return['Ad']['rght']));
+
+ $this->Ad->create();
+ $return = $this->Ad->save(array('campaign_id' => 2));
+ $this->assertEquals($expected, array($return['Ad']['lft'], $return['Ad']['rght']));
+ }
}

0 comments on commit 5bb7d73

Please sign in to comment.