cakephp behavior afterFind not called on related models

behavior, cakephp, cakephp-1.3, callback

Solution

I guess I'd do one of 2 things depending on how generically the code block applies:

- Universal version: not use a behavior, but include your method block in `AppModel::afterFind`

- Surgical version: use a behavior and attach it to each model that needs to share the functionality.

Problem

I am using an afterFind function to modify data from a find function. It works fine. If I move the afterFind function into a behavior (in a plugin) it still works, but only when the model of interest is the primary model, i.e. it isn't called when the model belongsTo another model. Is there any way round this? I'm using cake 1.3.4. This is a simplified version of the behavior: ``` class ChemicalStructureBehavior extends ModelBehavior { function afterFind(&$model, $results, $primary) { foreach ($results as &$unit) { // format chemical formula (with subscripts) $unit[$model->alias]['chemical_formula_formatted'] = preg_replace('/([0-9]+)/i', '<sub>$1</sub>', $unit[$model->alias]['chemical_formula']); } return $results; } } ```

Original source