Skip to content

Commit f0752fe

Browse files
committed
Adding patch from 'burzum'. Fixes ambiguous field sql error when joining two trees with a defined scope.
Test cases and fixtures added. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8178 3807eeeb-6ff5-0310-8944-8be069107fe0
1 parent 2cc00ac commit f0752fe

File tree

4 files changed

+140
-3
lines changed

4 files changed

+140
-3
lines changed

cake/libs/model/behaviors/tree.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,9 +877,10 @@ function __getMax($Model, $scope, $right, $recursive = -1, $created = false) {
877877
$scope['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
878878
}
879879
}
880+
$name = $Model->alias . '.' . $right;
880881
list($edge) = array_values($Model->find('first', array(
881882
'conditions' => $scope,
882-
'fields' => $db->calculate($Model, 'max', array($right)),
883+
'fields' => $db->calculate($Model, 'max', array($name, $right)),
883884
'recursive' => $recursive
884885
)));
885886
return (empty($edge[$right])) ? 0 : $edge[$right];
@@ -895,9 +896,10 @@ function __getMax($Model, $scope, $right, $recursive = -1, $created = false) {
895896
*/
896897
function __getMin($Model, $scope, $left, $recursive = -1) {
897898
$db =& ConnectionManager::getDataSource($Model->useDbConfig);
899+
$name = $Model->alias . '.' . $left;
898900
list($edge) = array_values($Model->find('first', array(
899901
'conditions' => $scope,
900-
'fields' => $db->calculate($Model, 'min', array($left)),
902+
'fields' => $db->calculate($Model, 'min', array($name, $left)),
901903
'recursive' => $recursive
902904
)));
903905
return (empty($edge[$left])) ? 0 : $edge[$left];

cake/tests/cases/libs/model/behaviors/tree.test.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ class ScopedTreeTest extends NumberTreeTest {
12291229
* @var array
12301230
* @access public
12311231
*/
1232-
var $fixtures = array('core.flag_tree', 'core.ad', 'core.campaign', 'core.translate');
1232+
var $fixtures = array('core.flag_tree', 'core.ad', 'core.campaign', 'core.translate', 'core.number_tree_two');
12331233
/**
12341234
* testStringScope method
12351235
*
@@ -1433,6 +1433,61 @@ function testTranslatingTree() {
14331433
);
14341434
$this->assertEqual($result, $expected);
14351435
}
1436+
/**
1437+
* testGenerateTreeListWithSelfJoin method
1438+
*
1439+
* @return void
1440+
* @access public
1441+
*/
1442+
function testAliasesWithScopeInTwoTreeAssociations() {
1443+
extract($this->settings);
1444+
$this->Tree =& new $modelClass();
1445+
$this->Tree->initialize(2, 2);
1446+
1447+
$this->TreeTwo =& new NumberTreeTwo();
1448+
1449+
$record = $this->Tree->find('first');
1450+
1451+
$this->Tree->bindModel(array(
1452+
'hasMany' => array(
1453+
'SecondTree' => array(
1454+
'className' => 'NumberTreeTwo',
1455+
'foreignKey' => 'number_tree_id'
1456+
)
1457+
)
1458+
));
1459+
$this->TreeTwo->bindModel(array(
1460+
'belongsTo' => array(
1461+
'FirstTree' => array(
1462+
'className' => $modelClass,
1463+
'foreignKey' => 'number_tree_id'
1464+
)
1465+
)
1466+
));
1467+
$this->TreeTwo->Behaviors->attach('Tree', array(
1468+
'scope' => 'FirstTree'
1469+
));
1470+
1471+
$data = array(
1472+
'NumberTreeTwo' => array(
1473+
'name' => 'First',
1474+
'number_tree_id' => $record['FlagTree']['id']
1475+
)
1476+
);
1477+
$this->TreeTwo->create();
1478+
$this->assertTrue($this->TreeTwo->save($data));
1479+
1480+
$result = $this->TreeTwo->find('first');
1481+
$expected = array('NumberTreeTwo' => array(
1482+
'id' => 1,
1483+
'name' => 'First',
1484+
'number_tree_id' => $record['FlagTree']['id'],
1485+
'parent_id' => null,
1486+
'lft' => 1,
1487+
'rght' => 2
1488+
));
1489+
$this->assertEqual($result, $expected);
1490+
}
14361491
}
14371492
/**
14381493
* AfterTreeTest class

cake/tests/cases/libs/model/models.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,28 @@ function initialize($levelLimit = 3, $childLimit = 3, $currentLevel = null, $par
22732273
}
22742274
}
22752275
}
2276+
/**
2277+
* NumberTreeTwo class
2278+
*
2279+
* @package cake
2280+
* @subpackage cake.tests.cases.libs.model
2281+
*/
2282+
class NumberTreeTwo extends NumberTree {
2283+
/**
2284+
* name property
2285+
*
2286+
* @var string 'NumberTree'
2287+
* @access public
2288+
*/
2289+
var $name = 'NumberTreeTwo';
2290+
/**
2291+
* actsAs property
2292+
*
2293+
* @var array
2294+
* @access public
2295+
*/
2296+
var $actsAs = array();
2297+
}
22762298
/**
22772299
* FlagTree class
22782300
*
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/* SVN FILE: $Id$ */
3+
/**
4+
* Tree behavior class.
5+
*
6+
* Enables a model object to act as a node-based tree.
7+
*
8+
* PHP versions 4 and 5
9+
*
10+
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
11+
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12+
*
13+
* Licensed under The Open Group Test Suite License
14+
* Redistributions of files must retain the above copyright notice.
15+
*
16+
* @filesource
17+
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18+
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
19+
* @package cake
20+
* @subpackage cake.tests.fixtures
21+
* @since CakePHP(tm) v 1.2.0.5331
22+
* @version $Revision$
23+
* @modifiedby $LastChangedBy$
24+
* @lastmodified $Date$
25+
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
26+
*/
27+
/**
28+
* Number Tree Test Fixture
29+
*
30+
* Generates a tree of data for use testing the tree behavior
31+
*
32+
* @package cake
33+
* @subpackage cake.tests.fixtures
34+
*/
35+
class NumberTreeTwoFixture extends CakeTestFixture {
36+
/**
37+
* name property
38+
*
39+
* @var string 'NumberTree'
40+
* @access public
41+
*/
42+
var $name = 'NumberTreeTwo';
43+
/**
44+
* fields property
45+
*
46+
* @var array
47+
* @access public
48+
*/
49+
var $fields = array(
50+
'id' => array('type' => 'integer','key' => 'primary'),
51+
'name' => array('type' => 'string','null' => false),
52+
'number_tree_id' => array('type' => 'integer', 'null' => false),
53+
'parent_id' => 'integer',
54+
'lft' => array('type' => 'integer','null' => false),
55+
'rght' => array('type' => 'integer','null' => false)
56+
);
57+
}
58+
?>

0 commit comments

Comments
 (0)