yii\base\Component::attachBehavior PHP Метод

attachBehavior() публичный Метод

This method will create the behavior object based on the given configuration. After that, the behavior object will be attached to this component by calling the [[Behavior::attach()]] method.
См. также: detachBehavior()
public attachBehavior ( string $name, string | array | Behavior $behavior ) : Behavior
$name string the name of the behavior.
$behavior string | array | Behavior the behavior configuration. This can be one of the following: - a [[Behavior]] object - a string specifying the behavior class - an object configuration array that will be passed to [[Yii::createObject()]] to create the behavior object.
Результат Behavior the behavior object
    public function attachBehavior($name, $behavior)
    {
        $this->ensureBehaviors();
        return $this->attachBehaviorInternal($name, $behavior);
    }

Usage Example

 protected function attachSupportBehaviors(Component $owner)
 {
     $rangeAttributes = [];
     foreach ($this->attributes as $attribute => $dbAttribute) {
         $rangeAttributes[] = $attribute . '_start';
         $rangeAttributes[] = $attribute . '_end';
         $owner->attachBehavior(0, ['class' => DateTimeRangeBehavior::class, 'startAttribute' => $attribute . '_start_local', 'endAttribute' => $attribute . '_end_local', 'targetAttribute' => $attribute . '_range']);
     }
     $owner->attachBehavior(0, ['class' => DateTimeBehavior::class, 'originalFormat' => ['date', 'yyyy-MM-dd'], 'targetFormat' => ['date', 'dd.MM.yyyy'], 'attributes' => $rangeAttributes]);
 }