Skip to content

Commit

Permalink
Adding patch from 'burzum'. Fixes ambiguous field sql error when join…
Browse files Browse the repository at this point in the history
…ing 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
  • Loading branch information
markstory committed May 26, 2009
1 parent 2cc00ac commit f0752fe
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
6 changes: 4 additions & 2 deletions cake/libs/model/behaviors/tree.php
Expand Up @@ -877,9 +877,10 @@ function __getMax($Model, $scope, $right, $recursive = -1, $created = false) {
$scope['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
}
}
$name = $Model->alias . '.' . $right;
list($edge) = array_values($Model->find('first', array(
'conditions' => $scope,
'fields' => $db->calculate($Model, 'max', array($right)),
'fields' => $db->calculate($Model, 'max', array($name, $right)),
'recursive' => $recursive
)));
return (empty($edge[$right])) ? 0 : $edge[$right];
Expand All @@ -895,9 +896,10 @@ function __getMax($Model, $scope, $right, $recursive = -1, $created = false) {
*/
function __getMin($Model, $scope, $left, $recursive = -1) {
$db =& ConnectionManager::getDataSource($Model->useDbConfig);
$name = $Model->alias . '.' . $left;
list($edge) = array_values($Model->find('first', array(
'conditions' => $scope,
'fields' => $db->calculate($Model, 'min', array($left)),
'fields' => $db->calculate($Model, 'min', array($name, $left)),
'recursive' => $recursive
)));
return (empty($edge[$left])) ? 0 : $edge[$left];
Expand Down
57 changes: 56 additions & 1 deletion cake/tests/cases/libs/model/behaviors/tree.test.php
Expand Up @@ -1229,7 +1229,7 @@ class ScopedTreeTest extends NumberTreeTest {
* @var array
* @access public
*/
var $fixtures = array('core.flag_tree', 'core.ad', 'core.campaign', 'core.translate');
var $fixtures = array('core.flag_tree', 'core.ad', 'core.campaign', 'core.translate', 'core.number_tree_two');
/**
* testStringScope method
*
Expand Down Expand Up @@ -1433,6 +1433,61 @@ function testTranslatingTree() {
);
$this->assertEqual($result, $expected);
}
/**
* testGenerateTreeListWithSelfJoin method
*
* @return void
* @access public
*/
function testAliasesWithScopeInTwoTreeAssociations() {
extract($this->settings);
$this->Tree =& new $modelClass();
$this->Tree->initialize(2, 2);

$this->TreeTwo =& new NumberTreeTwo();

$record = $this->Tree->find('first');

$this->Tree->bindModel(array(
'hasMany' => array(
'SecondTree' => array(
'className' => 'NumberTreeTwo',
'foreignKey' => 'number_tree_id'
)
)
));
$this->TreeTwo->bindModel(array(
'belongsTo' => array(
'FirstTree' => array(
'className' => $modelClass,
'foreignKey' => 'number_tree_id'
)
)
));
$this->TreeTwo->Behaviors->attach('Tree', array(
'scope' => 'FirstTree'
));

$data = array(
'NumberTreeTwo' => array(
'name' => 'First',
'number_tree_id' => $record['FlagTree']['id']
)
);
$this->TreeTwo->create();
$this->assertTrue($this->TreeTwo->save($data));

$result = $this->TreeTwo->find('first');
$expected = array('NumberTreeTwo' => array(
'id' => 1,
'name' => 'First',
'number_tree_id' => $record['FlagTree']['id'],
'parent_id' => null,
'lft' => 1,
'rght' => 2
));
$this->assertEqual($result, $expected);
}
}
/**
* AfterTreeTest class
Expand Down
22 changes: 22 additions & 0 deletions cake/tests/cases/libs/model/models.php
Expand Up @@ -2273,6 +2273,28 @@ function initialize($levelLimit = 3, $childLimit = 3, $currentLevel = null, $par
}
}
}
/**
* NumberTreeTwo class
*
* @package cake
* @subpackage cake.tests.cases.libs.model
*/
class NumberTreeTwo extends NumberTree {
/**
* name property
*
* @var string 'NumberTree'
* @access public
*/
var $name = 'NumberTreeTwo';
/**
* actsAs property
*
* @var array
* @access public
*/
var $actsAs = array();
}
/**
* FlagTree class
*
Expand Down
58 changes: 58 additions & 0 deletions cake/tests/fixtures/number_tree_two_fixture.php
@@ -0,0 +1,58 @@
<?php
/* SVN FILE: $Id$ */
/**
* Tree behavior class.
*
* Enables a model object to act as a node-based tree.
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.fixtures
* @since CakePHP(tm) v 1.2.0.5331
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
/**
* Number Tree Test Fixture
*
* Generates a tree of data for use testing the tree behavior
*
* @package cake
* @subpackage cake.tests.fixtures
*/
class NumberTreeTwoFixture extends CakeTestFixture {
/**
* name property
*
* @var string 'NumberTree'
* @access public
*/
var $name = 'NumberTreeTwo';
/**
* fields property
*
* @var array
* @access public
*/
var $fields = array(
'id' => array('type' => 'integer','key' => 'primary'),
'name' => array('type' => 'string','null' => false),
'number_tree_id' => array('type' => 'integer', 'null' => false),
'parent_id' => 'integer',
'lft' => array('type' => 'integer','null' => false),
'rght' => array('type' => 'integer','null' => false)
);
}
?>

0 comments on commit f0752fe

Please sign in to comment.