CakePHP: afterFind is weird with associations -
so afterfind works fine , dandy when i'm within corresponding model/controller. however, when calling associated model, data sent afterfind callback formatted differently. causes afterfind crap out because can't find same array indexes did when working within original model/controller.
anyone know why, or fix might be?
$primary may not helpful; i've found false when using containablebehaviour beyond first depth:
$this->model->find('first', array( 'contain' => array( 'secondarymodel' => array( 'tertiarymodel', ), ), )); if you're setting value based on related model, can check presence deal either structure this:
function afterfind($results, $primary) { if (isset($results['tertiarymodel'])) { $results['secondary_model_field'] = 'value'; } else { foreach ($results &$result) { if (is_array($result) && isset($result['tertiarymodel'])) { $result[$this->alias]['secondary_model_field'] = 'value'; } } unset($result); } return $results; } alternately may able check location of field on model itself. if field doesn't exist @ top level, need iterate on set of results.
Comments
Post a Comment