mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Making Helper::viewEntity accept any number of levels
This commit is contained in:
parent
31f0cdc214
commit
2941e89da3
5 changed files with 77 additions and 7 deletions
0
app/tmp/cache/models/empty
vendored
0
app/tmp/cache/models/empty
vendored
0
app/tmp/cache/persistent/empty
vendored
0
app/tmp/cache/persistent/empty
vendored
0
app/tmp/cache/views/empty
vendored
0
app/tmp/cache/views/empty
vendored
|
@ -382,17 +382,30 @@ class Helper extends Overloadable {
|
|||
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 @@ class Helper extends Overloadable {
|
|||
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)) {
|
||||
|
|
|
@ -592,5 +592,45 @@ class HelperTest extends CakeTestCase {
|
|||
$result = $this->Helper->clean('<script>alert(document.cookie)</script>');
|
||||
$this->assertEqual($result, '&lt;script&gt;alert(document.cookie)&lt;/script&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');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
?>
|
Loading…
Reference in a new issue