attach() public method

The default implementation will set the [[owner]] property and attach event handlers as declared in [[events]]. Make sure you call the parent implementation if you override this method.
public attach ( Component $owner )
$owner Component the component that this behavior is to be attached to.
Exemplo n.º 1
0
 public function attach($owner)
 {
     parent::attach($owner);
     $owner->on(BaseActiveRecord::EVENT_BEFORE_INSERT, [ClearCacheEvent::className(), ClearCacheEvent::EVENT_CLEAR_CACHE]);
     $owner->on(BaseActiveRecord::EVENT_BEFORE_UPDATE, [ClearCacheEvent::className(), ClearCacheEvent::EVENT_CLEAR_CACHE]);
     $owner->on(BaseActiveRecord::EVENT_BEFORE_DELETE, [ClearCacheEvent::className(), ClearCacheEvent::EVENT_CLEAR_CACHE]);
 }
 /**
  * @inheritdoc
  * @param Controller $owner
  */
 public function attach($owner)
 {
     if (false === $owner instanceof Controller) {
         throw new InvalidParamException('QueryLanguageBehaviour can only be attached to ' . 'instances of Controller');
     }
     parent::attach($owner);
 }
Exemplo n.º 3
0
 public function attach($owner)
 {
     parent::attach($owner);
     if (!$this->key) {
         $this->key = constant(get_class($owner) . '::CACHE_KEY');
     }
 }
Exemplo n.º 4
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     parent::attach($owner);
     $validators = $this->owner->getValidators();
     $validator = Validator::createValidator('safe', $this->owner, 'template_id');
     $validators->append($validator);
 }
Exemplo n.º 5
0
 /**
  * @inheritdoc
  * @param ActiveRecord $owner
  */
 public function attach($owner)
 {
     if (false === $owner instanceof ActiveRecord) {
         throw new InvalidParamException('SafeSaver can only be attached to ' . 'instances of ActiveRecord');
     }
     parent::attach($owner);
 }
Exemplo n.º 6
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     parent::attach($owner);
     $validators = $owner->validators;
     foreach ($this->rules as $rule) {
         if ($rule instanceof Validator) {
             $validators->append($rule);
             $this->validators[] = $rule;
             // keep a reference in behavior
         } elseif (is_array($rule) && isset($rule[0], $rule[1])) {
             // attributes, validator type
             $validator = Validator::createValidator($rule[1], $owner, (array) $rule[0], array_slice($rule, 2));
             $validators->append($validator);
             $this->validators[] = $validator;
             // keep a reference in behavior
         } else {
             throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
         }
     }
     $owner->on(ActiveRecord::EVENT_BEFORE_INSERT, function () {
         $this->saveRelatedDir($this->txt, $this->id_field, $this->name_field, $this->classDir);
     });
     $owner->on(ActiveRecord::EVENT_BEFORE_UPDATE, function () {
         $this->saveRelatedDir($this->txt, $this->id_field, $this->name_field, $this->classDir);
     });
 }
 public function attach($owner)
 {
     parent::attach($owner);
     if ($this->hasMethod('defaultScope')) {
         call_user_func([$owner->modelClass, 'defaultScope'], $owner);
     }
 }
 /**
  * @param ActiveRecord $owner
  */
 public function attach($owner)
 {
     parent::attach($owner);
     // Применяем конфигурационные опции
     foreach ($this->imageConfig as $key => $value) {
         $var = '_' . $key;
         $this->{$var} = $value;
     }
     // Вычисляем корень сайта
     if (empty($this->_rootPathAlias)) {
         $savePathParts = explode('/', $this->_savePathAlias);
         // Удаляем последнюю часть
         unset($savePathParts[count($savePathParts - 1)]);
         // Объединяем все части обратно
         $this->_rootPathAlias = implode('/', $savePathParts);
     }
     // Добавляем валидатор require
     if ($this->_imageRequire) {
         $owner->validators->append(Validator::createValidator(RequiredValidator::className(), $owner, $this->_imageAttribute));
     }
     // Подключаем валидатор изображения
     $validatorParams = array_merge(['extensions' => $this->_fileTypes, 'maxSize' => $this->_maxFileSize, 'skipOnEmpty' => true, 'tooBig' => 'Изображение слишком велико, максимальный размер: ' . floor($this->_maxFileSize / 1024 / 1024) . ' Мб'], $this->_imageValidatorParams);
     $validator = Validator::createValidator(ImageValidator::className(), $owner, $this->_imageAttribute, $validatorParams);
     $owner->validators->append($validator);
 }
Exemplo n.º 9
0
 /**
  * @param ActiveRecord $owner
  */
 public function attach($owner)
 {
     if (!$owner instanceof ActiveRecord) {
         throw new \InvalidArgumentException(sprintf('Behavior %s can only be attached to an instace of yii\\db\\ActiveRecord, %s given.', get_called_class(), is_object($owner) ? get_class($owner) : gettype($owner)));
     }
     parent::attach($owner);
 }
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     if (!in_array($owner->scenario, $this->scenarios)) {
         return;
     }
     if (!$owner instanceof PasswordInterface) {
         throw new InvalidConfigException('Class `' . get_class($owner) . '` must be an object implementing ' . '`PasswordInterface`');
     }
     parent::attach($owner);
     $oldPasswordAttribute = $this->oldPasswordAttribute;
     $newPasswordAttribute = $this->newPasswordAttribute;
     $confirmedPasswordAttribute = $this->confirmedPasswordAttribute;
     // required
     if (!$this->skipOnEmpty && !$this->checkPassword) {
         $validator = Validator::createValidator('required', $owner, [$newPasswordAttribute, $confirmedPasswordAttribute]);
         $owner->validators->append($validator);
     }
     // safe
     $attributes = [$newPasswordAttribute, $confirmedPasswordAttribute];
     if ($this->checkPassword) {
         $attributes[] = $oldPasswordAttribute;
     }
     $validator = Validator::createValidator('safe', $owner, $attributes);
     $owner->validators->append($validator);
 }
Exemplo n.º 11
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     if ($this->modelClass === null) {
         $this->modelClass = get_class($owner);
     }
     parent::attach($owner);
 }
 /**
  * Check if the behavior is attached to an Active Record
  * @param ActiveRecord $owner
  * @throws RuntimeException
  */
 public function attach($owner)
 {
     if (!$owner instanceof ActiveRecord) {
         throw new RuntimeException('Owner must be instance of yii\\db\\ActiveRecord');
     }
     parent::attach($owner);
 }
Exemplo n.º 13
0
 public function attach($model)
 {
     parent::attach($model);
     if (empty($this->fileInputName)) {
         $this->fileInputName = 'hydra_' . $this->attribute;
     }
 }
Exemplo n.º 14
0
 /**
  * @param \skeeks\cms\base\db\ActiveRecord $owner
  * @throws Exception
  */
 public function attach($owner)
 {
     if (!$owner instanceof \yii\db\ActiveRecord) {
         throw new Exception(\Yii::t('app', "This behavior is designed only to work with {class}", ['class' => \yii\db\ActiveRecord::className()]));
     }
     parent::attach($owner);
 }
Exemplo n.º 15
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     parent::attach($owner);
     $validators = $owner->validators;
     $validator = Validator::createValidator('safe', $owner, array_keys($this->attributes));
     $validators->append($validator);
 }
Exemplo n.º 16
0
 public function attach($owner)
 {
     parent::attach($owner);
     $validators = $owner->validators;
     $validator = Validator::createValidator('safe', $owner, ['singleImageArray']);
     $validators->append($validator);
 }
 /**
  * @param \yii\base\Component $owner
  * @throws InvalidConfigException
  */
 public function attach($owner)
 {
     parent::attach($owner);
     if ($owner && !$owner instanceof SeoModelInterface) {
         throw new InvalidConfigException('Owner must be implemented "app\\seo\\interfaces\\SeoModelInterface"');
     }
 }
Exemplo n.º 18
0
 public function attach($owner)
 {
     parent::attach($owner);
     if (!$this->owner->isNewRecord) {
         list($this->_p, $this->_wr, $this->_fn) = explode("\n", $this->owner->content);
     }
 }
Exemplo n.º 19
0
 /**
  * @param \skeeks\cms\Controller $owner
  * @throws Exception
  */
 public function attach($owner)
 {
     if (!$owner instanceof \skeeks\cms\Controller) {
         throw new Exception(\Yii::t('app', 'This behavior is designed only to work with {class}', ['class' => \skeeks\cms\Controller::className()]));
     }
     parent::attach($owner);
 }
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     if (!$owner instanceof Application) {
         throw new InvalidConfigException('Owner must be an application class');
     }
     parent::attach($owner);
 }
Exemplo n.º 21
0
 public function attach($owner)
 {
     parent::attach($owner);
     if ($this->owner->scenario == "search") {
         return true;
     }
     if (empty($this->initial)) {
         throw new Exception("It's required to set an initial state");
     }
     if (empty($this->model_label)) {
         throw new Exception("It's required to set a model label");
     }
     if (empty($this->namespace)) {
         $this->namespace = strtolower(get_class($this->owner)) . '\\status';
     }
     $this->options = array_keys($this->transitions);
     foreach ($this->transitions as $k => $t) {
         if (!is_array($t)) {
             $this->transitions[$k] = explode(",", $t);
         } else {
             $this->transitions[$k] = $t;
         }
     }
     $this->getStatus();
 }
 /**
  * @inheritdoc
  * @param Controller $owner
  */
 public function attach($owner)
 {
     if (false === $owner instanceof Controller) {
         throw new InvalidParamException('ResponseFormatBehavior can only be attached to ' . 'instances of Controller');
     }
     parent::attach($owner);
 }
 /**
  * @param \yii\base\Component $owner
  */
 public function attach($owner)
 {
     parent::attach($owner);
     if (!$this->group) {
         $this->group = get_class($this->owner);
     }
 }
Exemplo n.º 24
0
 /**
  * @param \yii\base\Component $owner
  * @throws InvalidConfigException
  */
 public function attach($owner)
 {
     if (!is_a($owner, 'yii\\db\\BaseActiveRecord')) {
         throw new InvalidConfigException('Le possesseur de ce behavior doit descendre de yii\\db\\BaseActiveRecord');
     }
     parent::attach($owner);
 }
Exemplo n.º 25
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     parent::attach($owner);
     if (!is_array($this->attributes) || empty($this->attributes)) {
         throw new InvalidParamException('Invalid or empty attributes array.');
     } else {
         foreach ($this->attributes as $attribute => $config) {
             if (!isset($config['path']) || empty($config['path'])) {
                 throw new InvalidParamException('Path must be set for all attributes.');
             }
             if (!isset($config['tempPath']) || empty($config['tempPath'])) {
                 throw new InvalidParamException('Temporary path must be set for all attributes.');
             }
             if (!isset($config['url']) || empty($config['url'])) {
                 $config['url'] = $this->publish($config['path']);
             }
             $this->attributes[$attribute]['path'] = FileHelper::normalizePath(Yii::getAlias($config['path'])) . DIRECTORY_SEPARATOR;
             $this->attributes[$attribute]['tempPath'] = FileHelper::normalizePath(Yii::getAlias($config['tempPath'])) . DIRECTORY_SEPARATOR;
             $this->attributes[$attribute]['url'] = rtrim($config['url'], '/') . '/';
             $validator = Validator::createValidator('string', $this->owner, $attribute);
             $this->owner->validators[] = $validator;
             unset($validator);
         }
     }
 }
Exemplo n.º 26
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     if (!$owner instanceof ActiveRecord) {
         throw new InvalidConfigException(Yii::t('vote', 'Please attach this behavior to the instance of the ActiveRecord class'));
     }
     parent::attach($owner);
 }
Exemplo n.º 27
0
 public function attach($owner)
 {
     parent::attach($owner);
     $owner->on(BaseActiveRecord::EVENT_BEFORE_INSERT, [AdminLogEvent::className(), AdminLogEvent::EVENT_CREATE_ITEM], ['title' => $this->titleAttribute, 'icon' => $this->icon]);
     $owner->on(BaseActiveRecord::EVENT_BEFORE_UPDATE, [AdminLogEvent::className(), AdminLogEvent::EVENT_UPDATE_ITEM], ['title' => $this->titleAttribute, 'icon' => $this->icon]);
     $owner->on(BaseActiveRecord::EVENT_BEFORE_DELETE, [AdminLogEvent::className(), AdminLogEvent::EVENT_DELETE_ITEM], ['title' => $this->titleAttribute, 'icon' => $this->icon]);
 }
Exemplo n.º 28
0
 public function attach($owner)
 {
     if (!$owner instanceof ShelterInterface) {
         throw new InvalidParamException('$owner must implement ShelterInterface.');
     }
     parent::attach($owner);
 }
 /** @inheritdoc */
 public function attach($owner)
 {
     if (!is_subclass_of($owner, BaseImagable::className())) {
         throw new Exception("The owner must be inherited from " . BaseImagable::className());
     }
     parent::attach($owner);
 }
Exemplo n.º 30
0
 public function attach($owner)
 {
     if (!$owner instanceof Request) {
         throw new InvalidConfigException('GeoBehavior can be only attached to the yii\\web\\Request and it\'s children');
     }
     parent::attach($owner);
 }