Browse Source

Avoiding SQL errors by prefixing columns with the Table alias

fixes #4508
Jose Lorenzo Rodriguez 11 years ago
parent
commit
06048e1669

+ 31 - 11
src/Model/Behavior/TreeBehavior.php

@@ -189,7 +189,7 @@ class TreeBehavior extends Behavior {
 		$max = $left - 1;
 
 		if ($left < $targetLeft) {
-			//Moving to the right
+			// Moving to the right
 			$targetLeft = $parentRight - $diff;
 			$targetRight = $parentRight - 1;
 			$min = $right + 1;
@@ -198,7 +198,7 @@ class TreeBehavior extends Behavior {
 		}
 
 		if ($right - $left > 1) {
-			//Correcting internal subtree
+			// Correcting internal subtree
 			$internalLeft = $left + 1;
 			$internalRight = $right - 1;
 			$this->_sync($targetLeft - $left, '+', "BETWEEN {$internalLeft} AND {$internalRight}", true);
@@ -210,7 +210,7 @@ class TreeBehavior extends Behavior {
 			$this->_unmarkInternalTree();
 		}
 
-		//Allocating new position
+		// Allocating new position
 		$entity->set($config['left'], $targetLeft);
 		$entity->set($config['right'], $targetRight);
 	}
@@ -280,13 +280,20 @@ class TreeBehavior extends Behavior {
 		}
 
 		$config = $this->config();
-		list($left, $right) = [$config['left'], $config['right']];
+		$alias = $this->_table->alias();
+		list($left, $right) = array_map(
+			function($field) use ($alias) {
+				return "$alias.$field";
+			},
+			[$config['left'], $config['right']]
+		);
+
 		$node = $this->_table->get($options['for'], ['fields' => [$left, $right]]);
 
 		return $this->_scope($query)
 			->where([
-				"$left <=" => $node->get($left),
-				"$right >=" => $node->get($right)
+				"$left <=" => $node->get($config['left']),
+				"$right >=" => $node->get($config['right'])
 			]);
 	}
 
@@ -300,7 +307,13 @@ class TreeBehavior extends Behavior {
  */
 	public function childCount(Entity $node, $direct = false) {
 		$config = $this->config();
-		list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
+		$alias = $this->_table->alias();
+		list($parent, $left, $right) = array_map(
+			function($field) use ($alias) {
+				return "$alias.$field";
+			},
+			[$config['parent'], $config['left'], $config['right']]
+		);
 
 		if ($direct) {
 			return $this->_scope($this->_table->find())
@@ -309,7 +322,7 @@ class TreeBehavior extends Behavior {
 		}
 
 		$this->_ensureFields($node);
-		return ($node->{$right} - $node->{$left} - 1) / 2;
+		return ($node->get($config['right']) - $node->get($config['left']) - 1) / 2;
 	}
 
 /**
@@ -330,8 +343,15 @@ class TreeBehavior extends Behavior {
  */
 	public function findChildren(Query $query, array $options) {
 		$config = $this->config();
+		$alias = $this->_table->alias();
 		$options += ['for' => null, 'direct' => false];
-		list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
+		list($parent, $left, $right) = array_map(
+			function($field) use ($alias) {
+				return "$alias.$field";
+			},
+			[$config['parent'], $config['left'], $config['right']]
+		);
+
 		list($for, $direct) = [$options['for'], $options['direct']];
 
 		if (empty($for)) {
@@ -349,8 +369,8 @@ class TreeBehavior extends Behavior {
 		$node = $this->_getNode($for);
 		return $this->_scope($query)
 			->where([
-				"{$right} <" => $node->{$right},
-				"{$left} >" => $node->{$left}
+				"{$right} <" => $node->get($config['right']),
+				"{$left} >" => $node->get($config['left'])
 			]);
 	}
 

+ 21 - 0
tests/TestCase/Model/Behavior/TreeBehaviorTest.php

@@ -756,6 +756,27 @@ class TreeBehaviorTest extends TestCase {
 	}
 
 /**
+ * Tests that using associations having tree fields in the schema
+ * does not generate SQL errors
+ *
+ * @return void
+ */
+	public function testFindPathWithAssociation() {
+		$table = $this->table;
+		$other = TableRegistry::get('FriendlyTrees', [
+			'table' => $table->table()
+		]);
+		$table->hasOne('FriendlyTrees', [
+			'foreignKey' => 'id'
+		]);
+		$result = $table
+			->find('children', ['for' => 1])
+			->contain('FriendlyTrees')
+			->toArray();
+		$this->assertCount(9, $result);
+	}
+
+/**
  * Custom assertion use to verify tha a tree is returned in the expected order
  * and that it is still valid
  *