Yii Framework Forum: How To Check If A Behavior Already Been Attached? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Check If A Behavior Already Been Attached? Rate Topic: -----

#1 User is offline   explorer 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 69
  • Joined: 26-July 13

Posted 19 October 2013 - 02:57 AM

Hi,

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()
   {
      parent::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?
0

#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 19 October 2013 - 11:20 AM

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

#3 User is offline   explorer 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 69
  • Joined: 26-July 13

Posted 19 October 2013 - 10:54 PM

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?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users