Behaviors on model

Hi there everybody,

Today I have a doubt: I have an advanced project with a backend and a REST api as a module. The models inside the REST module directly inherit from the ones used in the backend: something like:





namespace api\modules\v1\models;


class Analysis extends \common\models\Analysis {}




In the \common\models\Analysis model I have declared a date behavior that basically change the format from the mysql format (Y-m-d) to the standard italian way to represent dates (d-m-Y) and viceversa, depending on whether I am storing data on the db or retrieve for some backend views, implemented in this way:




class DateFormatBehavior extends Behavior {

  

  /**

   * @var array Lista di attributi da convertire 

   */

  public $attributes = [];


  /**

   * @inheritdoc

   */

  public function attach($owner) {

    parent::attach($owner);


    if (!$this->owner instanceof ActiveRecord) {

      throw new Exception("Questo behavior è applicabile sono a classi che estendono ActiveRecord");

    }

  }


  /**

   * @inheritdoc

   */

  public function events() {

    return [

      \yii\base\Model::EVENT_BEFORE_VALIDATE => 'validateDateFormat',

      ActiveRecord::EVENT_AFTER_FIND => 'findDateFormat'

    ];

  }




  /**

   * Prima di validare un modello esistente

   * Converte  il formato dd/MM/yyyy => yyyy-MM-dd

   */

  public function validateDateFormat() {

    foreach ($this->attributes as $attribute) {

      if (isset($this->owner->{$attribute})) {

        $old_value = $this->owner->getOldAttribute($attribute);

        if ($old_value != $this->owner->{$attribute}) {

          $this->owner->{$attribute} = FormatterDate::frontToDb($this->owner->{$attribute});

          if (!$this->owner->{$attribute}) {

            $this->owner->addError($attribute, 'Formato data errato');

          }

        }

      }

    }

  }


  /**

   * Dopo aver caricato il modello

   * Converte  il fomrato yyyy-MM-dd => dd/MM/yyyy 

   */

  public function findDateFormat() {

    foreach ($this->attributes as $attribute) {

      if (isset($this->owner->{$attribute})) {

        $this->owner->{$attribute} = FormatterDate::dbToFront($this->owner->{$attribute});

        if (!$this->owner->{$attribute}) {

          $this->owner->addError($attribute, 'Formato data errato');

        }

      }

    }

  }

}



FormatterDate is an helper class that convert bethween this two date format (sorry for the italian language comments but it should be quite clear anyway).

The behavior work pretty well on the backend whereas in the REST Api it simply do not invoke the findDateFormat on the ActiveRecord::EVENT_AFTER_FIND. with an action where the find() method is called in this way:





class ViewAnalysisAction extends Action {


  /**

   * Fornisce i dettagli di un'analisi

   * Prima di inviare i dati verifica che l'utente abbiamo un abbonamento attivo

   * 

   */

  public function run($symbol) {

    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

    

    //here is the find() that do not trigger the AFTER_FIND event <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />


    $model = \api\modules\v1\models\Analysis::find() 

        ->joinWith('category')

        ->where(['category.code' => $symbol])

        ->orderBy(['created_at' => SORT_DESC])

        ->asArray()

        ->one();


    if ($this->checkAccess) {

      call_user_func($this->checkAccess, $this->id, $model);

    }


    return $model ? $model : [];

  }


}



Unfortunately REST is stateless and have no session, meaning I cannot debug using Xdebug and inspect whether the event is triggered. I am struggling to get why, on the API, events are not triggered. The fact that the API model classes inherit from the backend models should ensure that also the behavior would be inherited right?

Hope somebody had my same problem and solve it somehow… thanks in advance and cheers! ;D