Skip to content

Commit

Permalink
Fix reading array values from associated entities.
Browse files Browse the repository at this point in the history
Split out conditions into temporary variables as the conditions would be
a bit batty otherwise.

Refs #4083
  • Loading branch information
markstory committed Jul 27, 2014
1 parent dc7fcb9 commit 13210fa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/View/Form/EntityContext.php
Expand Up @@ -260,18 +260,23 @@ protected function _getEntity($path) {
$path = array_slice($path, 1);
}

foreach ($path as $prop) {
$len = count($path);
$last = $len - 1;
for ($i = 0; $i < $len; $i++) {
$prop = $path[$i];
$next = $this->_getProp($entity, $prop);

if ($next === null && $prop !== '_ids') {
return false;
}

if (
!is_array($next) &&
!($next instanceof Traversable) &&
!($next instanceof Entity)
) {
$isLast = ($i === $last && isset($next));
$isTraversable = (
is_array($next) ||
$next instanceof Traversable ||
$next instanceof Entity
);
if ($isLast || !$isTraversable) {
return $entity;
}
$entity = $next;
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -404,6 +404,30 @@ public function testValBasic() {
$this->assertNull($result);
}

/**
* Test reading array values from an entity.
*
* @return void
*/
public function testValGetArrayValue() {
$row = new Article([
'title' => 'Test entity',
'types' => [1, 2, 3],
'author' => new Entity([
'roles' => ['admin', 'publisher']
])
]);
$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Articles',
]);
$result = $context->val('types');
$this->assertEquals($row->types, $result);

$result = $context->val('author.roles');
$this->assertEquals($row->author->roles, $result);
}

/**
* Test that val() reads from the request.
*
Expand Down

0 comments on commit 13210fa

Please sign in to comment.