本文整理汇总了PHP中yii\db\ActiveRecord::setAttribute方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::setAttribute方法的具体用法?PHP ActiveRecord::setAttribute怎么用?PHP ActiveRecord::setAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::setAttribute方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: saveFile
/**
* @return bool
* @throws \yii\base\Exception
*/
protected function saveFile()
{
if ($this->uploadFile && $this->uploadFile instanceof UploadedFile) {
if (!file_exists($this->uploadDir)) {
FileHelper::createDirectory($this->uploadDir, '0777');
}
if ($this->uploadFile->saveAs($this->getFilePath($this->getFileName()))) {
$this->owner->setAttribute($this->attribute, $this->getFileName());
return true;
}
} else {
$this->owner->setAttribute($this->attribute, $this->owner->getOldAttribute($this->attribute));
}
return false;
}
示例2: afterFind
/**
* 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));
}
}
}
示例3: setAttribute
/**
* @inheritdoc
*/
public function setAttribute($name, $value)
{
if ($this->tryToSetRelation($name, $value)) {
return;
}
parent::setAttribute($name, $value);
}
示例4: setAttribute
public function setAttribute($name, $value)
{
if (isset($this->{$name})) {
$this->{$name} = $value;
} else {
parent::setAttribute($name, $value);
}
}
示例5: loadModel
/**
* Populate model data
* @param ActiveRecord $model the model to populate
* @param array $modelData
* @return bool true if success
*/
public function loadModel($model, $modelData)
{
$model->trigger('beforeLoad');
if (empty($modelData)) {
return false;
}
$attributes = $model->attributes();
$b = false;
foreach ($attributes as $attrName) {
if (array_key_exists($attrName, $modelData)) {
$model->setAttribute($attrName, $modelData[$attrName]);
$b = true;
}
}
$model->trigger('afterLoad');
return $b;
}
示例6: performTransition
/**
* Perform last checks and the actual state transition.
*
* @param \yii\db\ActiveRecord|IStateful $model
* @param array $stateChange
* @param mixed $sourceState
* @param boolean $confirmed
*
* @return bool true if state transition has been performed
*/
public function performTransition($model, $stateChange, $sourceState, $confirmed = true)
{
// explicitly assign the new state value to avoid forcing the state attribute to be safe
$model->setAttribute($model->getStateAttributeName(), $this->targetState);
if (!$this->beforeTransition($model) || $model->performTransition() === false) {
return false;
}
$this->afterTransition($model);
return true;
}
示例7: beforeUpdate
/**
* Метод срабатывает в момент обновления существующей записи моедли.
*/
public function beforeUpdate()
{
if (in_array($this->owner->scenario, $this->scenarios)) {
foreach ($this->attributes as $attribute) {
if ($this->owner->isAttributeChanged($attribute)) {
$fileTempPath = $this->getTempPath($attribute);
if (is_file($fileTempPath)) {
rename($fileTempPath, $this->getPath($attribute));
if ($this->deleteOnSave === true and $this->owner->getOldAttribute($attribute)) {
$this->delete($attribute, true);
}
$this->triggerEventAfterUpload();
} else {
$this->owner->setAttribute($attribute, $this->owner->getOldAttribute($attribute));
}
}
}
}
// Удаляем указаные атрибуты и их файлы если это нужно
if (!empty($this->deleteScenarios) and in_array($this->owner->scenario, $this->deleteScenarios)) {
foreach ($this->deleteScenarios as $attribute => $scenario) {
if ($this->owner->scenario === $scenario) {
$file = $this->getPath($attribute);
if (is_file($file) and unlink($file)) {
$this->owner->{$attribute} = null;
}
}
}
}
}
示例8: setAttribute
public function setAttribute($name, $value)
{
if ($this->trySetupEmbeddedRelation($name, $value)) {
// if success
return;
}
parent::setAttribute($name, $value);
}
示例9: setSort
public function setSort($position)
{
$this->owner->setAttribute($this->attributeName, $position);
}
示例10: setAttribute
/**
* Sets the named attribute value.
* @param string $name
* the attribute name
* @param mixed $value
* the attribute value.
* @throws InvalidParamException if the named attribute does not exist.
* @throws Exception if the current record is read only
* @see hasAttribute()
*/
public function setAttribute($name, $value)
{
if ($this->getReadOnly()) {
throw new Exception('Attempting to set attribute `' . $name . '` on a read only ' . Tools::getClassName($this) . ' model');
}
parent::setAttribute($name, $value);
}