Refactored the filterResults again. Now this method not call the callback afterFind when model is Model.

This commit is contained in:
Juan Basso 2011-02-26 05:23:59 -03:00
parent 34b4ff9204
commit 63d700a9d6

View file

@ -1128,29 +1128,36 @@ class DboSource extends DataSource {
* @return array Array of results that have been filtered through $model->afterFind
*/
protected function _filterResults(&$results, Model $model, $filtered = array()) {
static $haveCallback = array();
$current = current($results);
if (!is_array($current)) {
return array();
}
$keys = array_diff(array_keys($current), $filtered, array($model->alias));
$filtering = array();
$_filtered = array_flip($filtered);
foreach ($results as &$result) {
if (is_array($result)) {
if (!isset($keys)) {
$keys = array_keys($result);
}
foreach ($keys as $className) {
if ($model->alias !== $className && !isset($_filtered[$className])) {
$filtering[] = $className;
if (isset($model->{$className}) && is_object($model->{$className})) {
$data = $model->{$className}->afterFind(array(array($className => $result[$className])), false);
}
if (isset($data[0][$className])) {
$result[$className] = $data[0][$className];
}
}
foreach ($keys as $className) {
if (!isset($model->{$className}) || !is_object($model->{$className})) {
continue;
}
$linkedModel = $model->{$className};
$linkedClass = get_class($linkedModel);
if (!isset($haveCallback[$linkedClass])) {
$ref = new ReflectionClass($linkedModel);
$haveCallback[$linkedClass] = $ref->getMethod('afterFind')->class !== 'Model';
}
if ($haveCallback[$linkedClass] !== true) {
continue;
}
$filtering[] = $className;
foreach ($results as &$result) {
$data = $linkedModel->afterFind(array(array($className => $result[$className])), false);
if (isset($data[0][$className])) {
$result[$className] = $data[0][$className];
}
}
}
return array_unique($filtering);
return $filtering;
}
/**