update() public méthode

This method performs the following steps in order: 1. call [[beforeValidate()]] when $runValidation is true. If [[beforeValidate()]] returns false, the rest of the steps will be skipped; 2. call [[afterValidate()]] when $runValidation is true. If validation failed, the rest of the steps will be skipped; 3. call [[beforeSave()]]. If [[beforeSave()]] returns false, the rest of the steps will be skipped; 4. save the record into database. If this fails, it will skip the rest of the steps; 5. call [[afterSave()]]; In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]], [[EVENT_AFTER_VALIDATE]], [[EVENT_BEFORE_UPDATE]], and [[EVENT_AFTER_UPDATE]] will be raised by the corresponding methods. Only the [[dirtyAttributes|changed attribute values]] will be saved into database. For example, to update a customer record: php $customer = Customer::findOne($id); $customer->name = $name; $customer->email = $email; $customer->update(); Note that it is possible the update does not affect any row in the table. In this case, this method will return 0. For this reason, you should use the following code to check if update() is successful or not: php if ($customer->update() !== false) { update successful } else { update failed }
public update ( boolean $runValidation = true, array $attributeNames = null ) : integer | false
$runValidation boolean whether to perform validation (calling [[validate()]]) before saving the record. Defaults to `true`. If the validation fails, the record will not be saved to the database and this method will return `false`.
$attributeNames array list of attributes that need to be saved. Defaults to `null`, meaning all attributes that are loaded from DB will be saved.
Résultat integer | false the number of rows affected, or false if validation fails or [[beforeSave()]] stops the updating process.
 /**
  * @inheritdoc
  */
 public function update($runValidation = true, $attributeNames = null)
 {
     $this->trigger(static::EVENT_AFTER_UPDATE_TRANSACTION);
     return parent::update($runValidation, $attributeNames);
 }
Exemple #2
0
    public function update($runValidation = true, $attributeNames = null){
        $this->image = UploadedFile::getInstance($this->user0, 'profile_pic');

        if(!parent::validate())
            return false;

        if($this->image instanceof  UploadedFile){

            if(!getimagesize($this->image->tempName)){
                $this->addError('image','Please Upload a valid Image');
                return false;
            }

            Auction::info('Image Upload Event Triggered');
            $this->trigger(Events::UPLOAD_IMAGE);

            parent::update(false);
            $this->user0->profile_pic = $this->image;
        }
        else{
            $this->user0->profile_pic = $this->user0->oldAttributes['profile_pic'];
        }

        if($this->user0->save(false)) {
            Auction::info('Dealer is successsfully Updated');
            return true;
        }else{
            Auction::infoLog('Dealer is not updated Due to following validation Errors',$this->user0->getErrors());
            return false;
        }
    }
 /**
  * 更新数据
  * @param bool $runValidation
  * @param null $attributeNames
  * @return bool|int
  * @throws \Exception
  */
 public function update($runValidation = true, $attributeNames = null)
 {
     $result = parent::update($runValidation, $attributeNames);
     if ($result) {
         if (self::enableCache()) {
             Yii::trace('delete cache:' . self::getCacheKey($this->getAttribute(static::$pk)), __METHOD__);
             Yii::$app->cache->delete(self::getCacheKey($this->getAttribute(static::$pk)));
             Yii::$app->cache->delete(self::getCacheKey($this->getAttribute(static::$pk), false));
         }
     }
     return $result;
 }
Exemple #4
0
 /**
  * prefering soft-delete instead of deleting permanently
  * adding delete time & blamable user
  *
  * @return boolean
  */
 public function softDelete()
 {
     /* simpan waktu delete (soft-delete) */
     if ($this->isSoftDeleteEnabled) {
         $this->setAttribute('deleted_at', time());
         if ($this->hasAttribute('deletedBy_id')) {
             $this->setAttribute('deletedBy_id', Yii::$app->user->getId());
         }
         return parent::update(FALSE);
     }
     /* delete record dr database (hard-delete) */
     return $this->hardDelete();
 }
Exemple #5
0
 public function update($runValidation = true, $attributeNames = NULL)
 {
     if (!empty($this->password)) {
         $this->password_hash = Yii::$app->security->generatePasswordHash($this->password);
     }
     $assignment = \backend\models\AuthAssignment::findOne(['user_id' => $this->id]);
     if ($assignment != null) {
         $assignment->item_name = $this->auth_item;
         if (!$assignment->update()) {
             print_r($assignment->getErrors());
             exit;
         }
     } else {
         $this->save_assignment($this->id);
     }
     $this->status = $this->status == 1 ? 10 : 0;
     parent::update();
     return true;
 }
 /**
  * (non-PHPdoc)
  * @see \yii\db\ActiveRecord::update()
  */
 public function update($runValidation = true, $attributeNames = null, $hasParentModel = false, $fromUpdateAll = false)
 {
     if ($this->getReadOnly() && !$hasParentModel) {
         // return failure if we are at the top of the tree and should not be asking to supdateAll
         // not allowed to amend or delete
         $message = 'Attempting to update on ' . Tools::getClassName($this) . ' readOnly model';
         //$this->addActionError($message);
         throw new Exception($message);
     } elseif ($this->getReadOnly() && $hasParentModel) {
         $message = 'Skipping update on ' . Tools::getClassName($this) . ' readOnly model';
         $this->addActionWarning($message);
         return true;
     } else {
         try {
             $ok = parent::update($runValidation, $attributeNames);
         } catch (\Exception $e) {
             $ok = false;
             $this->addActionError($e->getMessage(), $e->getCode());
         }
         return $ok;
     }
 }