Permalink
Browse files

Making Helper::viewEntity accept any number of levels

  • Loading branch information...
1 parent 31f0cdc commit 2941e89da35b2ddfba2a417322693d13cb4d37e4 @lorenzo lorenzo committed with markstory Oct 21, 2009
View
0 app/tmp/cache/models/empty
No changes.
View
0 app/tmp/cache/persistent/empty
No changes.
View
0 app/tmp/cache/views/empty
No changes.
View
44 cake/libs/view/helper.php
@@ -365,7 +365,7 @@ function setEntity($entity, $setScope = false) {
} elseif (join('.', $view->entity()) == $entity) {
return;
}
-
+
if ($entity === null) {
$view->model = null;
$view->association = null;
@@ -381,18 +381,31 @@ function setEntity($entity, $setScope = false) {
if (empty($parts)) {
return;
}
-
- if (count($parts) === 1 || is_numeric($parts[0])) {
+
+ $count = count($parts);
+ if ($count === 1) {
$sameScope = true;
} else {
- if (ClassRegistry::isKeySet($parts[0])) {
- $model = $parts[0];
+ if (is_numeric($parts[0])) {
+ $sameScope = true;
+ }
+ $reverse = array_reverse($parts);
+ $field = array_shift($reverse);
+ while(!empty($reverse)) {
+ $subject = array_shift($reverse);
+ if (is_numeric($subject)) {
+ continue;
+ }
+ if (ClassRegistry::isKeySet($subject)) {
+ $model = $subject;
+ break;
+ }
}
}
-
+
if (ClassRegistry::isKeySet($model)) {
$ModelObj =& ClassRegistry::getObject($model);
- for ($i = 0; $i < count($parts); $i++) {
+ for ($i = 0; $i < $count; $i++) {
if ($ModelObj->hasField($parts[$i]) || array_key_exists($parts[$i], $ModelObj->validate)) {
$hasField = $i;
if ($hasField === 0 || ($hasField === 1 && is_numeric($parts[0]))) {
@@ -451,6 +464,23 @@ function setEntity($entity, $setScope = false) {
list($view->association, $view->modelId, $view->field, $view->fieldSuffix) = $parts;
}
break;
+ default:
+ $reverse = array_reverse($parts);
+
+ if ($hasField) {
+ $view->field = $field;
+ if (!is_numeric($reverse[1]) && $reverse[1] != $model) {
+ $view->field = $reverse[1];
+ $view->fieldSuffix = $field;
+ }
+ }
+ if (is_numeric($parts[0])) {
+ $view->modelId = $parts[0];
+ } elseif ($view->model == $parts[0] && is_numeric($parts[1])) {
+ $view->modelId = $parts[1];
+ }
+ $view->association = $model;
+ break;
}
if (!isset($view->model) || empty($view->model)) {
View
40 cake/tests/cases/libs/view/helper.test.php
@@ -592,5 +592,45 @@ function testClean() {
$result = $this->Helper->clean('&lt;script&gt;alert(document.cookie)&lt;/script&gt;');
$this->assertEqual($result, '&amp;lt;script&amp;gt;alert(document.cookie)&amp;lt;/script&amp;gt;');
}
+
+ /**
+ * testMultiDimensionalField method
+ *
+ * @access public
+ * @return void
+ */
+ function testMultiDimensionalField() {
+ // PHP4 reference hack
+ ClassRegistry::removeObject('view');
+ ClassRegistry::addObject('view', $this->View);
+
+ $this->Helper->setEntity('HelperTestPost', true);
+
+ $this->Helper->setEntity('HelperTestPost.2.HelperTestComment.1.title');
+ $this->assertEqual($this->View->model, 'HelperTestPost');
+ $this->assertEqual($this->View->association, 'HelperTestComment');
+ $this->assertEqual($this->View->modelId,2);
+ $this->assertEqual($this->View->field, 'title');
+
+ $this->Helper->setEntity('HelperTestPost.1.HelperTestComment.1.HelperTestTag.1.created');
+ $this->assertEqual($this->View->field,'created');
+ $this->assertEqual($this->View->association,'HelperTestTag');
+ $this->assertEqual($this->View->modelId,1);
+
+
+ $this->Helper->setEntity('HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake');
+ $this->assertEqual($this->View->model,'HelperTestPost');
+ $this->assertEqual($this->View->association,'HelperTestTag');
+ $this->assertEqual($this->View->field,null);
+
+ $this->Helper->setEntity('1.HelperTestComment.1.HelperTestTag.created.year');
+ $this->assertEqual($this->View->model,'HelperTestPost');
+ $this->assertEqual($this->View->association,'HelperTestTag');
+ $this->assertEqual($this->View->field,'created');
+ $this->assertEqual($this->View->modelId,1);
+ $this->assertEqual($this->View->fieldSuffix,'year');
+ }
+
+
}
?>

0 comments on commit 2941e89

Please sign in to comment.