Browse Source

Fix issue with HABTM fields

habtm fields were not being correctly being detected.
mark_story 14 years ago
parent
commit
1743eaa776
2 changed files with 37 additions and 12 deletions
  1. 24 2
      lib/Cake/Test/Case/View/HelperTest.php
  2. 13 10
      lib/Cake/View/Helper.php

+ 24 - 2
lib/Cake/Test/Case/View/HelperTest.php

@@ -304,7 +304,6 @@ class HelperTest extends CakeTestCase {
 		$this->assertEquals($expected, $this->Helper->entity());
 
 		$this->assertEquals('HelperTestComment', $this->Helper->model());
-		
 	}
 
 /**
@@ -314,7 +313,11 @@ class HelperTest extends CakeTestCase {
  * @return void
  */
 	public function testSetEntityAssociatedCamelCaseField() {
-		$this->Helper->fieldset = array('HelperTestComment' => array('fields' => array('BigField' => 'something')));
+		$this->Helper->fieldset = array(
+			'HelperTestComment' => array(
+				'fields' => array('BigField' => array('type' => 'integer'))
+			)
+		);
 		$this->Helper->setEntity('HelperTestComment', true);
 		$this->Helper->setEntity('HelperTestComment.BigField');
 
@@ -323,6 +326,25 @@ class HelperTest extends CakeTestCase {
 	}
 
 /**
+ * Test that multiple fields work when they are camelcase and in fieldset
+ *
+ * @return void
+ */
+	public function testSetEntityAssociatedCamelCaseFieldHabtmMultiple() {
+		$this->Helper->fieldset = array(
+			'HelperTestComment' => array(
+				'fields' => array('Tag' => array('type' => 'multiple'))
+			)
+		);
+		$this->Helper->setEntity('HelperTestComment', true);
+		$this->Helper->setEntity('Tag');
+
+		$this->assertEquals('Tag', $this->Helper->model());
+		$this->assertEquals('Tag', $this->Helper->field());
+		$this->assertEquals(array('Tag', 'Tag'), $this->Helper->entity());
+	}
+
+/**
  * test that 'view' doesn't break things.
  *
  * @return void

+ 13 - 10
lib/Cake/View/Helper.php

@@ -454,23 +454,26 @@ class Helper extends Object {
 
 		$this->_association = null;
 
-		// check for associated model.
-		$reversed = array_reverse($parts);
-		foreach ($reversed as $part) {
-			if (empty($this->fieldset[$this->_modelScope]['fields'][$part]) && preg_match('/^[A-Z]/', $part)) {
-				$this->_association = $part;
-				break;
-			}
-		}
-
 		// habtm models are special
 		if (
 			isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
 			$this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
 		) {
+			$this->_association = $parts[0];
 			$entity = $parts[0] . '.' . $parts[0];
+		} else {
+			// check for associated model.
+			$reversed = array_reverse($parts);
+			foreach ($reversed as $part) {
+				if (
+					!isset($this->fieldset[$this->_modelScope]['fields'][$part]) &&
+					preg_match('/^[A-Z]/', $part)
+				) {
+					$this->_association = $part;
+					break;
+				}
+			}
 		}
-
 		$this->_entityPath = $entity;
 		return;
 	}