Ovewriting afterFind() vs. using Behaviors EVENT_AFTER_FIND


i like to modify the data after loading them from datastore (here: elastic search) using ActiveRecord.
The modification depends on some attributes of the model: $model::$counter. My data model ($model) extends \yii\[..your db engine..]\ActiveRecord

So it looks like, there are 2 ways:

1.) overwriting the afterFind method coming from ActiveRecord. The problem is, that attributes from my data-model ($model), like $model->counter are ignored by afterFind(). They need to be static attributes, like $model::$counter

2.) Defining an Behaviors, like explained here: Key Concepts: Behaviors | The Definitive Guide to Yii 2.0 | Yii PHP Framework

What is the “right” way / place for modifying data after find?
Does Behaviors support afertFind to work with non-static attributes, like $model->counter =1 ?

Thank you

  1. I would say no difference
     * This method is called when the AR object is created and populated with the query result.
     * The default implementation will trigger an [[EVENT_AFTER_FIND]] event.
     * When overriding this method, make sure you call the parent implementation to ensure the
     * event is triggered.
    public function afterFind()

You may want to try the attributes property