setAttribute() public method

Set a given attribute on the model.
public setAttribute ( string $key, mixed $value )
$key string
$value mixed
コード例 #1
1
ファイル: MorphOneOrMany.php プロジェクト: jBOKA/library
 /**
  * Removes a model from this relationship type.
  */
 public function remove(Model $model, $sessionKey = null)
 {
     if ($sessionKey === null) {
         $options = $this->parent->getRelationDefinition($this->relationName);
         if (array_get($options, 'delete', false)) {
             $model->delete();
         } else {
             /*
              * Make this model an orphan ;~(
              */
             $model->setAttribute($this->getPlainForeignKey(), null);
             $model->setAttribute($this->getPlainMorphType(), null);
             $model->save();
         }
         /*
          * Use the opportunity to set the relation in memory
          */
         if ($this instanceof MorphOne) {
             $this->parent->setRelation($this->relationName, null);
         } else {
             $this->parent->reloadRelations($this->relationName);
         }
     } else {
         $this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
     }
 }
コード例 #2
0
ファイル: ProfileUpdater.php プロジェクト: quetzyg/foundation
 /**
  * Save user profile.
  *
  * @param  \Orchestra\Model\User|\Illuminate\Database\Eloquent\Model  $user
  * @param  array  $input
  *
  * @return void
  */
 protected function saving($user, array $input)
 {
     $user->setAttribute('email', $input['email']);
     $user->setAttribute('fullname', $input['fullname']);
     $this->fireEvent('updating', [$user]);
     $this->fireEvent('saving', [$user]);
     $user->saveOrFail();
     $this->fireEvent('updated', [$user]);
     $this->fireEvent('saved', [$user]);
 }
コード例 #3
0
 /**
  * Removes a model from this relationship type.
  */
 public function remove(Model $model, $sessionKey = null)
 {
     if ($sessionKey === null) {
         // Make this model an orphan ;~(
         $model->setAttribute($this->getPlainForeignKey(), null);
         $model->setAttribute($this->getPlainMorphType(), null);
         $model->save();
     } else {
         $this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
     }
 }
コード例 #4
0
ファイル: model.php プロジェクト: rtablada/profane
 /**
  * Set a given attribute on the model.
  * Check if filtered first.
  *
  * @param  string  $key
  * @param  mixed   $value
  * @return void
  */
 public function setAttribute($key, $value)
 {
     if (in_array($key, $this->filtered)) {
         $value = \Filter::filter($value, $this->filterReplace);
     }
     parent::setAttribute($key, $value);
 }
コード例 #5
0
ファイル: ActiveModel.php プロジェクト: phpffcms/ffcms-core
 /**
  * Set model attribute. Extend laravel attribute casting mutators by serialized array
  * @param string $key
  * @param mixed $value
  * @return LaravelModel
  */
 public function setAttribute($key, $value)
 {
     if ($value !== null && $this->isSerializeCastable($key)) {
         $value = $this->asSerialize($value);
     }
     return parent::setAttribute($key, $value);
 }
コード例 #6
0
ファイル: EavValues.php プロジェクト: sohailaammarocs/lfc
 /**
  * Merges EAV "values" into the entity model, for easy access and
  * manipulation.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @param  \Illuminate\Database\Eloquent\Collection  $collection
  * @return void
  */
 protected function mergeValues(Model $model, Collection $values)
 {
     foreach ($values as $value) {
         $attribute = $value->getRelation($value->getAttributeRelation());
         $model->setAttribute($attribute->getAttributeKey(), $value->getValueKey());
     }
 }
コード例 #7
0
ファイル: User.php プロジェクト: EstebanJesus/bancopedagogico
 /**
  * Overrides the method to ignore the remember token.
  */
 public function setAttribute($key, $value)
 {
     $isRememberTokenAttribute = $key == $this->getRememberTokenName();
     if (!$isRememberTokenAttribute) {
         parent::setAttribute($key, $value);
     }
 }
コード例 #8
0
 /**
  * Set a given attribute on the model.
  *
  * @param  string $key
  * @param  mixed $value
  * @return $this
  */
 public function setAttribute($key, $value)
 {
     if (isset($this->mutations[$key])) {
         unset($this->mutations[$key]);
     }
     return parent::setAttribute($key, $value);
 }
コード例 #9
0
ファイル: HasOne.php プロジェクト: czim/laravel-pxlcms
 /**
  * Attach a model instance to the parent model.
  *
  * This adds the field_id value
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function save(Model $model)
 {
     if ($this->fieldId) {
         $model->setAttribute($this->fieldKey, $this->fieldId);
     }
     return parent::save($model);
 }
コード例 #10
0
 /**
  * Removes a model from this relationship type.
  */
 public function remove(Model $model, $sessionKey = null)
 {
     if ($sessionKey === null) {
         $options = $this->parent->getRelationDefinition($this->relationName);
         if (array_get($options, 'delete', false)) {
             $model->delete();
         } else {
             // Make this model an orphan ;~(
             $model->setAttribute($this->getPlainForeignKey(), null);
             $model->setAttribute($this->getPlainMorphType(), null);
             $model->save();
         }
     } else {
         $this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
     }
 }
コード例 #11
0
ファイル: HasOneWrapper.php プロジェクト: tacone/datasource
 public function saveAfter(Model $child)
 {
     // this is what `$this->relation->save($child)` does
     // we inline it here to wrap the save() method
     $child->setAttribute($this->relation->getPlainForeignKey(), $this->relation->getParentKey());
     $result = DataSource::make($child)->save();
     return $result;
 }
コード例 #12
0
 /**
  * Convert an instance to an eloquent model object.
  *
  * @param \Illuminate\Database\Eloquent\Model $eloquentModel
  * @param array $name
  * @return void
  */
 public function toEloquentObject(EloquentModel &$eloquentModel, $name)
 {
     // get model data
     $dict = ['mapping' => $eloquentModel->getMapping()];
     foreach ($dict['mapping']['embeddeds'][$name]['attributes'] as $attribute => $column) {
         $eloquentModel->setAttribute($column, $this->{$attribute});
     }
 }
コード例 #13
0
ファイル: AdminModel.php プロジェクト: despark/ignicms
 /**
  * Override set attributes so we can check for empty strings.
  * @todo We need a different way to achieve this
  * @param string $key
  * @param mixed $value
  * @return $this
  */
 public function setAttribute($key, $value)
 {
     parent::setAttribute($key, $value);
     // TODO: Change the autogenerated stub
     if (isset($this->attributes[$key]) && is_string($this->attributes[$key]) && strlen($this->attributes[$key]) === 0) {
         $this->attributes[$key] = null;
     }
     return $this;
 }
コード例 #14
0
ファイル: SavableTrait.php プロジェクト: shopalicious/support
 protected function saveModel(Eloquent $model, array $data)
 {
     $data = Arr::except($data, ['_token']);
     foreach ($data as $key => $value) {
         $model->setAttribute($key, $value);
     }
     $model->save();
     return $model;
 }
コード例 #15
0
 /**
  * Add the slug when not explicitly stated.
  *
  * @param  Model  $model
  * @return void
  */
 public function saving(Model $model)
 {
     $hasManuallyAssignedSlug = $model->slug && $model->isDirty('slug');
     if (!$model->isDirty('name') || $hasManuallyAssignedSlug) {
         return;
     }
     $slug = str_slug($model->getAttribute($this->fieldToSlug));
     $model->setAttribute($this->slugAttribute, $slug);
 }
コード例 #16
0
 /**
  * @inheritdoc
  *
  * @SuppressWarnings(PHPMD.StaticAccess)
  */
 public function setAttribute($key, $value)
 {
     // we want to be able to set dates in ISO8601. Laravel has an issue with
     // build-in format so we convert input manually.
     // For more see https://github.com/laravel/framework/issues/12203
     if (is_string($value) && (in_array($key, $this->getDates()) || $this->isDateCastable($key))) {
         $value = Carbon::createFromFormat(self::DATE_TIME_FORMAT, $value);
     }
     return parent::setAttribute($key, $value);
 }
コード例 #17
0
 /**
  * Save a new model and attach it to the parent model.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @return \Illuminate\Database\Eloquent\Model
  */
 protected function performInsert(Model $model)
 {
     // Create a new key if needed.
     if (!$model->getAttribute('_id')) {
         $model->setAttribute('_id', new MongoId());
     }
     $result = $this->query->update(array($this->localKey => $model->getAttributes()));
     if ($result) {
         $this->associate($model);
     }
     return $result ? $model : false;
 }
コード例 #18
0
ファイル: Model.php プロジェクト: apollopy/laravel-x-eloquent
 /**
  * Set a given attribute on the model.
  *
  * @param  string $key
  * @param  mixed  $value
  * @return $this
  */
 public function setAttribute($key, $value)
 {
     if (is_null($value) || !$this->isExtensionCastable($key) || $this->hasSetMutator($key)) {
         return parent::setAttribute($key, $value);
     }
     switch ($this->getCastType($key)) {
         case 'base64':
             $value = base64_encode($value);
             break;
     }
     $this->attributes[$key] = $value;
     return $this;
 }
コード例 #19
0
ファイル: Model.php プロジェクト: newmarkets/content
 /**
  * @doc inherit
  */
 public function setAttribute($key, $value)
 {
     // Let parent handle mutators
     if ($this->hasSetMutator($key)) {
         parent::setAttribute($key, $value);
     } else {
         if (in_array($key, $this->getDates()) && $value == '') {
             $this->attributes[$key] = null;
         } else {
             parent::setAttribute($key, $value);
         }
     }
 }
コード例 #20
0
 /**
  * Removes a model from this relationship type.
  */
 public function remove(Model $model, $sessionKey = null)
 {
     if ($sessionKey === null) {
         $model->setAttribute($this->getPlainForeignKey(), null);
         $model->save();
         /*
          * Use the opportunity to set the relation in memory
          */
         if ($this instanceof HasOne) {
             $this->parent->setRelation($this->relationName, null);
         }
     } else {
         $this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
     }
 }
コード例 #21
0
ファイル: Model.php プロジェクト: Jchedev/laravel-evolved
 /**
  * A BelongsTo relation can set using the dynamic setter (still need to be fillable) and boolean are automatically casted
  *
  * @param string $key
  * @param mixed $value
  * @return $this|bool|null
  */
 public function setAttribute($key, $value)
 {
     if (method_exists($this, 'set' . ucfirst(camel_case($key)) . 'Attribute') === false) {
         if (method_exists($this, $key) === true) {
             $relation = $this->{$key}();
             if (is_a($relation, BelongsTo::class)) {
                 $relation->associate($value);
                 return $this;
             }
         }
         if (array_get($this->casts, $key) == 'boolean') {
             $value = $value === true ? 1 : ($value === false ? 0 : $value);
         }
     }
     return parent::setAttribute($key, $value);
 }
コード例 #22
0
ファイル: BaseModel.php プロジェクト: errogaht/bv-table
 /**
  * Преобразование на запись
  *
  * @param string $key
  * @param mixed  $value
  * @return $this|void
  */
 public function setAttribute($key, $value)
 {
     if (isset($this->set_mutators[$key])) {
         $types = explode('|', $this->set_mutators[$key]);
         foreach ($types as $type) {
             switch ($type) {
                 case 'trim':
                     $value = trim($value);
                     break;
                 case 'lower':
                     $value = mb_strtolower($value, 'UTF-8');
                     break;
                 case 'ucfirst':
                     $firstChar = mb_substr($value, 0, 1, 'UTF-8');
                     $then = mb_substr($value, 1, mb_strlen($value, 'UTF-8') - 1, 'UTF-8');
                     $value = mb_strtoupper($firstChar, 'UTF-8') . mb_strtolower($then, 'UTF-8');
                     break;
                 case 'int':
                     $value = (int) $value;
                     break;
                 case 'real':
                 case 'float':
                 case 'double':
                     $value = (double) str_replace(',', '.', $value);
                     break;
                 case 'db_bool':
                     $value = strtoupper($value);
                     $value = 'Y' == $value || true === $value || '1' === $value || 1 === $value ? 'Y' : 'N';
                     break;
                 case 'phone':
                     $value = str_replace(['+', ' ', '(', ')', '-'], '', $value);
                     if (10 == strlen($value)) {
                         $value = '7' . $value;
                     }
                     if (11 == strlen($value) && '8' == $value[0]) {
                         $value[0] = '7';
                     }
                     break;
                 default:
                     break;
             }
         }
     }
     parent::setAttribute($key, $value);
 }
コード例 #23
0
 /**
  * Save a new model and attach it to the parent model.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function performInsert(Model $model)
 {
     // Generate a new key if needed.
     if ($model->getKeyName() == '_id' and !$model->getKey()) {
         $model->setAttribute('_id', new MongoId());
     }
     // For deeply nested documents, let the parent handle the changes.
     if ($this->isNested()) {
         $this->associate($model);
         return $this->parent->save();
     }
     $result = $this->getBaseQuery()->update(array($this->localKey => $model->getAttributes()));
     // Attach the model to its parent.
     if ($result) {
         $this->associate($model);
     }
     return $result ? $model : false;
 }
コード例 #24
0
ファイル: HandleFiles.php プロジェクト: anavel/crud
 /**
  * @param Request $request
  * @param Model   $item
  * @param array   $fields
  * @param $groupName
  * @param $fieldName
  * @param bool $mustDeleteFilesInFilesystem
  *
  * @throws \Exception
  *
  * @return array
  */
 protected function handleField(Request $request, $item, array $fields, $groupName, $fieldName, $mustDeleteFilesInFilesystem = false)
 {
     $normalizeGroupName = str_replace('.', '#', $groupName);
     $contentFromUploadedField = "uploaded-content.{$normalizeGroupName}#{$fieldName}#";
     $modelFolder = $this->slug . DIRECTORY_SEPARATOR;
     $basePath = base_path(DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . config('anavel-crud.uploads_path'));
     $modelPath = $basePath . $modelFolder;
     $skip = null;
     $requestValue = null;
     if (!empty($fields["{$fieldName}__delete"])) {
         //We never want to save this field, it doesn't exist in the DB
         $skip = "{$fieldName}__delete";
         //If user wants to delete the existing file
         if (!empty($request->input("{$groupName}.{$fieldName}__delete"))) {
             $adapter = new Local($basePath);
             $filesystem = new Filesystem($adapter);
             if ($filesystem->has($item->{$fieldName}) && $mustDeleteFilesInFilesystem) {
                 $filesystem->delete($item->{$fieldName});
             }
             $item->setAttribute($fieldName, null);
             return ['skip' => $skip];
         }
     }
     if ($request->has($contentFromUploadedField)) {
         $requestValue = $request->input($contentFromUploadedField);
     } elseif ($request->hasFile($groupName . '.' . $fieldName)) {
         $fileName = pathinfo($request->file($groupName . '.' . $fieldName)->getClientOriginalName(), PATHINFO_FILENAME);
         $extension = pathinfo($request->file($groupName . '.' . $fieldName)->getClientOriginalName(), PATHINFO_EXTENSION);
         $fileName = uniqid() . '_' . slugify($fileName);
         if (!empty($extension)) {
             $fileName .= '.' . $extension;
         }
         $request->file($groupName . '.' . $fieldName)->move($modelPath, $fileName);
         $requestValue = $modelFolder . $fileName;
     } elseif (!empty($request->file($groupName . '.' . $fieldName)) && !$request->file($groupName . '.' . $fieldName)->isValid()) {
         throw new \Exception($request->file($groupName . '.' . $fieldName)->getErrorMessage());
     }
     //Avoid losing the existing filename if the user doesn't change it:
     if (empty($requestValue) && !empty($item->{$fieldName})) {
         $requestValue = $item->{$fieldName};
     }
     return ['requestValue' => $requestValue, 'skip' => $skip];
 }
コード例 #25
0
 public function setAttribute($key, $value)
 {
     // Check if key is actually a relationship
     /** @var Relation $relation */
     if (method_exists($this, $key)) {
         // If so, convert scalars and arrays to instances of the correct model
         if (isset($value) && !$value instanceof Model) {
             /** @var Relation $relation */
             $relation = $this->{$key}();
             // Convert arrays to instances of the correct model
             if (is_array($value)) {
                 if (isset($value[0]) && is_scalar($value[0])) {
                 } else {
                     $value = $relation->getRelated()->newInstance($value);
                 }
             }
             if ($relation instanceof BelongsTo) {
                 $relation->associate($value);
             } else {
                 $this->setRelation($key, $value);
             }
             return;
         }
     }
     parent::setAttribute($key, $value);
 }
コード例 #26
0
 /**
  * Attach a model instance to the parent model.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function save(Model $model)
 {
     $model->setAttribute($this->getPlainForeignKey(), $this->getParentKey());
     return $model->save() ? $model : false;
 }
コード例 #27
0
ファイル: MorphOneOrMany.php プロジェクト: scrobot/Lumen
 /**
  * Attach a model instance to the parent model.
  *
  * @param  \Illuminate\Database\Eloquent\Model $model
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function save(Model $model)
 {
     $model->setAttribute($this->getPlainMorphType(), $this->morphClass);
     return parent::save($model);
 }
コード例 #28
0
 /**
  * Updates a model with the latest permissions, links, and fields
  *
  * @param \Illuminate\Database\Eloquent\Model		$model
  * @param \Frozennode\Administrator\Fields\Factory	$fieldFactory
  * @param \Frozennode\Administrator\Actions\Factory	$actionFactory
  *
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function updateModel($model, FieldFactory $fieldFactory, ActionFactory $actionFactory)
 {
     //set the data model to the active model
     $this->setDataModel($model->find($model->getKey()));
     //include the item link if one was supplied
     if ($link = $this->getModelLink()) {
         $model->setAttribute('admin_item_link', $link);
     }
     //set up the model with the edit fields new data
     $model->setAttribute('administrator_edit_fields', $fieldFactory->getEditFieldsArrays(true));
     //set up the new actions data
     $model->setAttribute('administrator_actions', $actionFactory->getActionsOptions(true));
     $model->setAttribute('administrator_action_permissions', $actionFactory->getActionPermissions(true));
     return $model;
 }
コード例 #29
0
ファイル: Model.php プロジェクト: RTLer/laravel-mongodb
 /**
  * Set a given attribute on the model.
  *
  * @param  string  $key
  * @param  mixed   $value
  */
 public function setAttribute($key, $value)
 {
     // Convert _id to MongoId.
     if ($key == '_id' and is_string($value)) {
         $builder = $this->newBaseQueryBuilder();
         $value = $builder->convertKey($value);
     } elseif (str_contains($key, '.')) {
         if (in_array($key, $this->getDates()) && $value) {
             $value = $this->fromDateTime($value);
         }
         array_set($this->attributes, $key, $value);
         return;
     }
     parent::setAttribute($key, $value);
 }
コード例 #30
0
ファイル: BaseModel.php プロジェクト: spira/api-core
 /**
  * @param string $key
  * @param mixed $value
  * @return $this
  * @throws SetRelationException
  */
 public function setAttribute($key, $value)
 {
     if (in_array($key, $this->getDates()) && $value) {
         if (!$value instanceof Carbon && !$value instanceof \DateTime) {
             $value = new Carbon($value);
             $this->attributes[$key] = $value;
             return;
         }
     }
     return parent::setAttribute($key, $value);
 }