Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding patch from 'burzum'. Fixes ambiguous field sql error when join…

…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...
commit f0752feeec1b8ba0703ee72f3be605e9491d8de4 1 parent 2cc00ac
@markstory markstory authored
View
6 cake/libs/model/behaviors/tree.php
@@ -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];
@@ -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];
View
57 cake/tests/cases/libs/model/behaviors/tree.test.php
@@ -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
*
@@ -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
View
22 cake/tests/cases/libs/model/models.php
@@ -2274,6 +2274,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
*
* @package cake
View
58 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)
+ );
+}
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.