While experimenting with Set::extract I ran into an situation where
I'm getting, what appears to be, incorrect results on the first
element (index 0) when using a back reference (i.e. "/x/..") in the
path when the set contains a calculated field.
Here is a simplified example to provide context:
The data set after calling Model->find(...) to get records with a
calculated field (i.e. "age")
$a = array(
[0] => array(
[Person] => array(
[name]=>Test 1,
[score]=>4
),
[0] => array(
[age] => 20
)
),
[1] => array(
[Person] => array(
[name]=>Test 2,
[score]=>5
),
[0] => array(
[age] => 21
)
),
[2] => array(
[Person] => array(
[name]=>Test 3,
[score]=>3
),
[0] => array(
[age] => 22
)
),
[3] => array(
[Person] => array(
[name]=>Test 4,
[score]=>2
),
[0] => array(
[age] => 16
)
)
);
If I do something like this:
$b = Set::extract('/0[age>19]/..', $a);
or
$b = Set::extract( '/Person[score>2]/..', $a);
I get this:
Array (
[0] => Array (
[age] => 20
)
[1] => Array (
[Person] => Array (
[name] => Test 2
[score] => 5
)
[0] => Array (
[age] => 21
)
)
[2] => Array (
[Person] => Array (
[name] => Test 3
[score] => 3
)
[0] => Array (
[age] => 22
)
)
)
Notice that the first item (at index 0 of the results returned from
extract) has lost everything but the calculated field (which also has/
had an index of 0).
If I put an empty element at the beginning of the data set ($a) (to
bump everything up by 1 index number) by doing something like:
array_unshift($a, array())
then I get the expected result which is:
Array (
[0] => Array (
[Person] => Array (
[name] => Test 1
[score] => 4
)
[0] => Array (
[age] => 20
)
)
[1] => Array (
[Person] => Array (
[name] => Test 2
[score] => 5
)
[0] => Array (
[age] => 21
)
)
[2] => Array (
[Person] => Array (
[name] => Test 3
[score] => 3
)
[0] => Array (
[age] => 22
)
)
)
Similarly, if I omit the back reference and just do:
$b = Set::extract( '/Person[score>2]', $a);
I get the expected results (e.g. all "Person" data without the first
item losing any info).
It appears to me that if there is a calculated field (which, as
expected, gets put into index 0 of the results returned from the find
call); Set::extract gets confused when getting the parent data for the
first item (at index 0) of the result set and erroneously returns the
data at $a[0][0] rather than the actual/correct data at $a[0].
So, before I cry "bug", Is my usage and expectation of the results in
such a situation correct? If not, what would be the correct way to
implement this using Set::extract?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---