How To Check If A Behavior Already Been Attached?


I need to attach some behaviors dynamically because they have the same methods but different implementation. To reduce the overhead of attaching the same behavior multiple times I need to check if a behavior had already been attached. I have tried to get the current behavior using ‘asa’ method, but it always returns null even when I call Video::model()->findAll().

My code is like this:

class Video extends CActiveRecord


   public function afterFind()



      $behavior = $this->videoSite . 'Behavior';

      $curBehavior = $this->asa('siteBehavior');

      var_dump($curBehavior);  // this always returns null


      if (is_null($curBehavior) || !($curBehavior instanceof $behavior))

         $this->attachBehavior('siteBehavior', $behavior);



Is there something that I miss here?

$this->attachBehavior() applies only to the current model instance, not to the whole class. I think it’s the problem :)

Thanks for your reply.

I see, so I think it’s useless to check whether the behavior had been attached (in my case) because it’s attached to each model instance?