Related tables updated - a Behavior

Hi, I’ve just discovered that Yii needs an Object to updated related tables when you save/update the main one. I’ve looked into extensions page where there are:

Now I’d prefer to use a component behavior, but CAdvancedArBehavior has only MANY_MANY capability, it doesn’t work with BELONGS_TO and HAS_ONE, as widget do. Further more, the widget seems a cleaner code.

So I would like to write a behavior taking code from the widget. Now my questions:

  1. Does exist a component behavior that makes those update?

  2. After I’ve created a behavior and attached to my Active model with:

public function behaviors(){

          return array( 'MyBehavior' => array(

            'class' => ''));


How can I access to Behavior’s methods from Controller? I’ve tried $this->MyBehavior->method() but it does nothing

  1. If I create a method that exists in model where behavior is attached, for example save() method, it overwrites model’s one?

If you want to call a method defined in the behavior, you call that method like a regular one.

class MyBehavior extends CBehavior


    public function TheMethod()

    { ... }


// suppose the behavior is attached to the object in $myObject.

// call the behavior's method TheMethod() like this (if $myObject does not have a method

// TheMethod):


// or like this (mind the case, it must exactly match the behavior name) if $myObject already has a method TheMethod():


So when you tried $this->Mybehavior->method() you were on the right track. If nothing happens then your behavior’s method probably has an error. If yii would find an error with the behavior then an exception is thrown and you’d see a message.