public function hasAttribute($name) { if (!empty($this->{$name})) { return true; } return parent::hasAttribute($name); }
/** * Handle 'afterFind' event of the owner. */ public function afterFind() { if ($this->owner->isRelationPopulated('translations') && ($related = $this->owner->getRelatedRecords()['translations'])) { $translations = $this->indexByLanguage($related); foreach ($this->availableLanguages as $lang) { foreach ($this->attributes as $attribute) { foreach ($translations as $translation) { if ($translation->{$this->languageField} == $lang) { $this->setTranslateAttribute($attribute . '_' . $lang, $translation->{$attribute}); if ($lang == Yii::$app->language) { $this->setTranslateAttribute($attribute, $translation->{$attribute}); } } } } } } else { if (!$this->owner->isRelationPopulated('translation')) { $this->owner->translation; } $translation = $this->owner->getRelatedRecords()['translation']; if ($translation) { foreach ($this->attributes as $attribute) { $this->owner->setTranslateAttribute($attribute, $translation->{$attribute}); } } } foreach ($this->attributes as $attribute) { if ($this->owner->hasAttribute($attribute) && $this->getTranslateAttribute($attribute)) { $this->owner->setAttribute($attribute, $this->getTranslateAttribute($attribute)); } } }
public function hasAttribute($name) { $class = get_class($this); if (!isset(self::$hasAttributeCache[$class][$name])) { self::$hasAttributeCache[$class][$name] = parent::hasAttribute($name); } return self::$hasAttributeCache[$class][$name]; }
/** * Проверка входящих параметров * * @throws \yii\base\Exception */ private function validateInputParams() { if (is_null($this->model)) { throw new Exception('Параметр model не задан'); } if (!$this->model instanceof ActiveRecord) { throw new Exception('Параметр model должен быть объектом класса ActiveRecord'); } if (!is_null($this->author)) { if (!$this->author instanceof ActiveRecord) { throw new Exception('Параметр author должен быть объектом класса ActiveRecord'); } if (!is_null($this->authorNameAttribute) && !$this->author->hasAttribute($this->authorNameAttribute)) { throw new Exception('Параметр author не содержит свойство ' . $this->authorNameAttribute); } if (!is_null($this->authorAvatarAttribute) && !$this->author->hasAttribute($this->authorAvatarAttribute)) { throw new Exception('Параметр author не содержит свойство ' . $this->authorAvatarAttribute); } } }
/** * @inheritdoc * @return array * @throws InvalidConfigException */ public function saveTempFiles() { if ($this->key === null) { throw new InvalidConfigException('You must specify key'); } if ($this->saveDirectoryName === null) { throw new InvalidConfigException('You must specify saveDirectoryName'); } if ($this->saveFileModel === null) { throw new InvalidConfigException('You must specify save model'); } if (!$this->saveFileModel instanceof ActiveRecord) { throw new InvalidConfigException('Save model must be instance of ActiveRecord'); } if ($this->saveFileAttribute === null) { throw new InvalidConfigException('You must specify save attribute'); } if ($this->saveFileRelatedAttribute === null) { throw new InvalidConfigException('You must specify related attribute'); } if (!$this->saveFileModel->hasAttribute($this->saveFileAttribute)) { throw new InvalidConfigException("{$this->saveFileModel->className()} has no attribute name {$this->saveFileAttribute}"); } $files = Yii::$app->session->get($this->key, []); foreach ($files as $file) { $oldPath = $file['path']; $newPath = Yii::$app->params['uploadsPath'] . "/{$this->saveDirectoryName}/" . ($newPath = "{$file['name']}"); if (file_exists($oldPath)) { if (copy($oldPath, $newPath)) { $fileModel = new $this->saveFileModel([$this->saveFileAttribute => $file['name'], $this->saveFileRelatedAttribute => $this->messageModel->id]); $fileModel->save(); } } } $this->clearTempFolder(); }
/** * @param ActiveRecord|HasProperties $model * @param string $idAttribute * @return bool */ public function appendToObjectModel(ActiveRecord $model, $idAttribute = 'id') { $object = Object::getForClass($model::className()); if (null === $object || !$model->hasAttribute($idAttribute)) { return false; } $link = new ObjectPropertyGroup(); $link->object_id = $object->id; $link->object_model_id = $model->{$idAttribute}; $link->property_group_id = $this->id; $result = $link->save(); $model->updatePropertyGroupsInformation(); return $result; }
/** * Determine status of one email AR. Be called after every email sent. * Result statuc can be "succeed", "failed" or "retry". * @param CActiveRecord $ar * @param bool $isSent */ protected function processEmailStatus(\yii\db\ActiveRecord &$mail, $isSent) { if (!($mail->hasAttribute($this->retryAttr) && $this->retryTimes > 0)) { $mail->{$this->statusAttr} = $isSent ? self::ST_SUCCEED : self::ST_FAILED; return; } if ($isSent) { if ($mail->{$this->statusAttr} == self::ST_RETRY) { // Current is a retry sending. $mail->{$this->retryAttr}++; } $mail->{$this->statusAttr} = self::ST_SUCCEED; } else { if ($mail->{$this->statusAttr} == self::ST_NEVER) { // First sending and retryable $mail->{$this->retryAttr} = 0; $mail->{$this->statusAttr} = self::ST_RETRY; } elseif (++$mail->{$this->retryAttr} < $this->retryTimes && $mail->{$this->statusAttr} == self::ST_RETRY) { // Retry sending and still retryable for next time } else { // Final failed. $mail->{$this->statusAttr} = self::ST_FAILED; } } }
/** * Returns a value indicating whether the model has an attribute with the specified name. * @param string $name the name of the attribute * @return boolean whether the model has an attribute with the specified name. */ public function hasAttribute($name) { $result = parent::hasAttribute($name); if (!$this->applyDefaults || $this->defaultsApplied) { return $result; } if ($this->getIsNewRecord() && $result) { $this->applyDefaults(false); } return $result; }
/** * 根据$addr以及跟新数据更新收货地址 * * $addr = self::find()->where($condition)->one(); * * @param \yii\db\ActiveRecord|\yii\db\ActiveRecord[] $addr 收货地址model对象 * @param array $data 更新数据 * ~ * [ * 'id' => '主键ID', 'addressee' => '', 'mobile' => '', * 'county' => '', 'address' => '', 'post_code' => '', * ] * ~ * * @return bool */ public function updateAddr($addr, $data) { foreach ($data as $field => $val) { if ($addr->hasAttribute($field)) { $addr->{$field} = $val; } } $addr->update_time = time(); return $addr->save(); }