diff --git a/cake/libs/set.php b/cake/libs/set.php index bff7b2868..a2ad13465 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -418,22 +418,25 @@ class Set extends Object { $match = false; if (array_key_exists($token, $context['item']) && (!$conditions || Set::matches($conditions, $context['item'][$token], $i))) { - $match = array( - 'trace' => am($context['trace'], $context['key']), - 'key' => $token, - 'item' => $context['item'][$token], - ); + $items = $context['item'][$token]; + if (!is_array($items) || !isset($items[0])) { + $items = array($items); + } + + foreach ($items as $item) { + $matches[] = array( + 'trace' => am($context['trace'], $context['key']), + 'key' => $token, + 'item' => $item, + ); + } } else if ($key === $token && (!$conditions || Set::matches($conditions, $context['item'], $i+1))) { - $match = array( + $matches[] = array( 'trace' => am($context['trace'], $key), 'key' => $key, 'item' => $context['item'], ); } - - if ($match) { - $matches[] = $match; - } } if (empty($tokens)) { break; diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 319ac3960..d30866ab9 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -423,6 +423,59 @@ class SetTest extends UnitTestCase { $expected = array(4); $r = Set::extract('/User/id', $single); $this->assertEqual($r, $expected); + + $common = array( + array( + 'Article' => array( + 'id' => 1, + 'name' => 'Article 1', + ), + 'Comment' => array( + array( + 'id' => 1, + 'user_id' => 5, + 'article_id' => 1, + 'text' => 'Comment 1', + ), + array( + 'id' => 2, + 'user_id' => 23, + 'article_id' => 1, + 'text' => 'Comment 2', + ), + array( + 'id' => 3, + 'user_id' => 17, + 'article_id' => 1, + 'text' => 'Comment 3', + ), + ), + ), + array( + 'Article' => array( + 'id' => 2, + 'name' => 'Article 2', + ), + 'Comment' => array( + array( + 'id' => 4, + 'user_id' => 2, + 'article_id' => 2, + 'text' => 'Comment 4', + ), + array( + 'id' => 5, + 'user_id' => 3, + 'article_id' => 2, + 'text' => 'Comment 5', + ), + ), + ) + ); + + $r = Set::extract('/Comment/id', $common); + $expected = array(1, 2, 3, 4, 5); + $this->assertEqual($r, $expected); } /** * undocumented function