Browse Source

Adding error to find(threaded).

When the model has no parent_id trigger a warning about the impending
failure and return an empty result.

Fixes #2341
mark_story 14 years ago
parent
commit
b4faa00703
2 changed files with 19 additions and 4 deletions
  1. 7 0
      lib/Cake/Model/Model.php
  2. 12 4
      lib/Cake/Test/Case/Model/ModelReadTest.php

+ 7 - 0
lib/Cake/Model/Model.php

@@ -2818,6 +2818,13 @@ class Model extends Object implements CakeEventListener {
 			foreach ($results as $result) {
 				$result['children'] = array();
 				$id = $result[$this->alias][$this->primaryKey];
+				if (!isset($result[$this->alias]['parent_id'])) {
+					trigger_error(
+						__d('cake_dev', 'You cannot use find("threaded") on models without a "parent_id" field.'),
+						E_USER_WARNING
+					);
+					return $return;
+				}
 				$parentId = $result[$this->alias]['parent_id'];
 				if (isset($idMap[$id]['children'])) {
 					$idMap[$id] = array_merge($result, (array)$idMap[$id]);

+ 12 - 4
lib/Cake/Test/Case/Model/ModelReadTest.php

@@ -2987,14 +2987,22 @@ class ModelReadTest extends BaseModelTest {
 		$noAfterFindData = $noAfterFindModel->find('all');
 
 		$this->assertFalse($afterFindModel == $noAfterFindModel);
-		// Limitation of PHP 4 and PHP 5 > 5.1.6 when comparing recursive objects
-		if (PHP_VERSION === '5.1.6') {
-			$this->assertFalse($afterFindModel != $duplicateModel);
-		}
 		$this->assertEquals($afterFindData, $noAfterFindData);
 	}
 
 /**
+ * find(threaded) should trigger errors whne there is no parent_id field.
+ *
+ * @expectedException PHPUnit_Framework_Error_Warning
+ * @return void
+ */
+	public function testFindThreadedError() {
+		$this->loadFixtures('Apple', 'Sample');
+		$Apple = new Apple();
+		$Apple->find('threaded');
+	}
+
+/**
  * testFindAllThreaded method
  *
  * @return void