From 25870441bc47b3eff5322498845573c3af98ca21 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 25 Jun 2011 13:10:09 -0400 Subject: [PATCH] Fixing '2.name' style fields when creating multiple record forms. --- lib/Cake/Test/Case/View/HelperTest.php | 13 ++++++++++--- lib/Cake/View/Helper.php | 22 ++++++++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/Cake/Test/Case/View/HelperTest.php b/lib/Cake/Test/Case/View/HelperTest.php index b818061f6..c2d9349d3 100644 --- a/lib/Cake/Test/Case/View/HelperTest.php +++ b/lib/Cake/Test/Case/View/HelperTest.php @@ -276,6 +276,11 @@ class HelperTest extends CakeTestCase { $expected = array('HelperTestPost', 'body'); $this->assertEquals($expected, $this->View->entity()); + + $this->Helper->setEntity('2.body'); + $expected = array('HelperTestPost', '2', 'body'); + $this->assertEquals($expected, $this->View->entity()); + $this->Helper->setEntity('Something.else'); $expected = array('Something', 'else'); $this->assertEquals($expected, $this->View->entity()); @@ -548,9 +553,11 @@ class HelperTest extends CakeTestCase { $expected = array('HelperTestPost'); $this->assertEquals($expected, $this->View->entity()); - $this->Helper->setEntity('date.month'); - $expected = array('HelperTestPost', 'date', 'month'); - $this->assertEquals($expected, $this->View->entity()); + foreach (array('year', 'month', 'day', 'hour', 'minute', 'meridian') as $d) { + $this->Helper->setEntity('date.' . $d); + $expected = array('HelperTestPost', 'date', $d); + $this->assertEquals($expected, $this->View->entity()); + } } /** diff --git a/lib/Cake/View/Helper.php b/lib/Cake/View/Helper.php index 4d2e03b1a..ae9fb40a3 100644 --- a/lib/Cake/View/Helper.php +++ b/lib/Cake/View/Helper.php @@ -416,6 +416,13 @@ class Helper extends Object { ) { $entity = $view->modelScope . '.' . $entity; } + if ( + $count === 2 && + is_numeric($parts[0]) && + !is_numeric($parts[1]) + ) { + $entity = $view->modelScope . '.' . $entity; + } $view->entityPath = $entity; return; @@ -567,11 +574,8 @@ class Helper extends Object { * @return string */ public function model() { - if (!empty($this->_View->association)) { - return $this->_View->association; - } else { - return $this->_View->model; - } + $entity = $this->_View->entity(); + return isset($entity[0]) ? $entity[0] : null; } /** @@ -589,7 +593,13 @@ class Helper extends Object { * @return string */ public function field() { - return $this->_View->field; + $entity = $this->_View->entity(); + $count = count($entity); + $last = $entity[$count - 1]; + if (in_array($last, $this->_fieldSuffixes)) { + $last = isset($entity[$count - 2]) ? $entity[$count - 2] : null; + } + return $last; } /**