From 593c4d724581f646bc617d75cf0461b708cbd162 Mon Sep 17 00:00:00 2001 From: the_undefined Date: Sun, 13 Apr 2008 13:05:52 +0000 Subject: [PATCH] Fixed issues with Set::extract and multiple numerically indexed nestings git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6656 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/set.php | 23 +++++++------ cake/tests/cases/libs/set.test.php | 53 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 10 deletions(-) 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