Thursday, January 10, 2013

Issue with removing records (unset) from data array in afterFind

Hi All

I'm experiencing a problem that I can't unset records from the data[] in the afterFind callback when the model a related model.
To show my issue clearly I've add this aferFind function to a model (assume I'd like to filter all returned records out for some reason):

public function afterFind($results, $primary = false) {
  $results = parent::afterFind($results, $primary);
  unset($results[0]);
  return $results;
}

This works fine for primary models, however not for related models (record is not unset).
I've traced it back to this part of the _filterResults function in DBOSource.php:

1128    foreach ($results as &$result) {
1129         $data = $linkedModel->afterFind(array(array($className => $result[$className])), false);
1130         if (isset($data[0][$className])) {
1131             $result[$className] = $data[0][$className];
1132         }
1133     }

Line 1130 just ignores the record unset since $data[0][$className] is not set - it was just unset!

If I change the code like this, it works for both primary and related:

1128    foreach ($results as $key => &$result) {
1129         $data = $linkedModel->afterFind(array(array($className => $result[$className])), false);
1130         if (isset($data[0][$className])) {
1131             $result[$className] = $data[0][$className];
1132          else {
1134            unset($results[$key]);
1135        }
1133     }

Am I doing something wrong or is this a cake issue?

Thanks!
Kevin

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: