Skip to content

Commit fb476f1

Browse files
committed
Fixing Set::extract run against hasMany records that are the result of a find('first'). Test case added.
1 parent 86f2008 commit fb476f1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

cake/libs/set.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ function extract($path, $data = null, $options = array()) {
387387
$options = array_merge(array('flatten' => true), $options);
388388
if (!isset($contexts[0])) {
389389
$current = current($data);
390-
if ((is_array($current) && count($data) <= 1) || !is_array($current)) {
390+
if ((is_array($current) && count($data) <= 1) || !is_array($current) || !Set::numeric(array_keys($data))) {
391391
$contexts = array($data);
392392
}
393393
}

cake/tests/cases/libs/set.test.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,24 @@ function testExtract() {
993993
$expected = array(array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'));
994994
$r = Set::extract('/file/.[type=application/zip]', $f);
995995
$this->assertEqual($r, $expected);
996+
997+
$hasMany = array(
998+
'Node' => array(
999+
'id' => 1,
1000+
'name' => 'First',
1001+
'state' => 50
1002+
),
1003+
'ParentNode' => array(
1004+
0 => array(
1005+
'id' => 2,
1006+
'name' => 'Second',
1007+
'state' => 60,
1008+
)
1009+
)
1010+
);
1011+
$result = Set::extract('/ParentNode/name', $hasMany);
1012+
$expected = array('Second');
1013+
$this->assertEqual($result, $expected);
9961014
}
9971015
/**
9981016
* testMatches method

0 commit comments

Comments
 (0)