Fixed bug in parent traversal, closes #5225

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7546 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2008-09-03 15:40:25 +00:00
parent 2deb6f3828
commit 95947053fe
2 changed files with 57 additions and 1 deletions

View file

@ -397,7 +397,8 @@ class Set extends Object {
$context = array('trace' => array(), 'item' => $context, 'key' => null); $context = array('trace' => array(), 'item' => $context, 'key' => null);
} }
if ($token == '..') { if ($token == '..') {
$context['item'] = Set::extract(join('/', $context['trace']), $data); $parent = join('/', $context['trace']).'['.($key+1).']';
$context['item'] = Set::extract($parent, $data);
$context['key'] = array_pop($context['trace']); $context['key'] = array_pop($context['trace']);
$context['item'] = $context['item'][0][$context['key']]; $context['item'] = $context['item'][0][$context['key']];
$matches[] = $context; $matches[] = $context;

View file

@ -633,6 +633,61 @@ class SetTest extends CakeTestCase {
$expected = array(array('Comment' => $common[1]['Comment'][0])); $expected = array(array('Comment' => $common[1]['Comment'][0]));
$r = Set::extract('/Comment[addition=]', $common); $r = Set::extract('/Comment[addition=]', $common);
$this->assertEqual($r, $expected); $this->assertEqual($r, $expected);
$habtm = array(
array(
'Post' => array(
'id' => 1,
'title' => 'great post',
),
'Comment' => array(
array(
'id' => 1,
'text' => 'foo',
'User' => array(
'id' => 1,
'name' => 'bob'
),
),
array(
'id' => 2,
'text' => 'bar',
'User' => array(
'id' => 2,
'name' => 'tod'
),
),
),
),
array(
'Post' => array(
'id' => 2,
'title' => 'fun post',
),
'Comment' => array(
array(
'id' => 3,
'text' => '123',
'User' => array(
'id' => 3,
'name' => 'dan'
),
),
array(
'id' => 4,
'text' => '987',
'User' => array(
'id' => 4,
'name' => 'jim'
),
),
),
),
);
$r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
$this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
$this->assertEqual($r[1]['Comment']['User']['name'], 'tod');
$this->assertEqual(count($r), 2);
} }
/** /**
* testMatches method * testMatches method