config[$model->name] = am( array( 'simplifyIfJoins' => true // Do we want to simplify the result // list if there are joined models? ), $config ); if (!isset($model->useSimpleResults)) { // Simple switch in the model to enable per-action deactivation of this feature $model->useSimpleResults = true; } } function afterFind(&$model, $results) { // If switch has been disabled then cancel if (!$model->useSimpleResults) { return; } // Skip empty arrays if (empty($results)) { return; } // The results must be a numerically-indexed list (0..n) if (!$this->_isNumericArray($results)) { return; } // The resultset must reference the model itself (sanity check) if (!isset($results[0][$model->name])) { return; } // If the resultset contains joins then // we need to check the config to see if it's allowed if ( (!$this->config[$model->name]['simplifyIfJoins']) && (sizeof($results[0]) > 1) ) { return; } $out = array(); foreach ($results as $result) { // Grab the self-model reference $base = $result[$model->name]; // Remove the self-model reference from the results unset($result[$model->name]); // Append these (if any) to the self-model results $out[] = am($base, $result); } // Return the reorganized results return array( "{$model->name}" => $out ); } // This handy function I wrote is actually part of my standard // includes loaded in bootstrap.php - but I'll put it here instead /** * Check if an array is numerically indexed in a standard manner. * [0..(n-1)], with no other keys * * @param array $array Array to check * @return boolean */ function _isNumericArray($array) { if (!is_array($array)) { return null; } return (array_sum(array_keys($array)) === (sizeof($array) * (sizeof($array)-1))>>1) } } ?>