Exemple #1
0
 /**
  * Run Create/Read/Update/Delete action
  *
  * Set error flash and throw Exception, if action failed
  * Return nothing if action succeed
  *
  * @param \yii\db\ActiveRecord|\yii\db\ActiveRecord[] $model
  * @return void
  * @throws \Exception
  */
 protected function runCrudAction($model)
 {
     if ($model instanceof Model) {
         if (!call_user_func([$model, $this->modelAction])) {
             if ($model->hasErrors()) {
                 $this->flashError = [];
                 foreach ($model->getErrors() as $errors) {
                     $this->flashError = array_merge($this->flashError, $errors);
                 }
             }
             throw new \Exception($this->flashError ? VarDumper::dumpAsString($this->flashError) : 'Unknown error');
         }
         return;
     }
     foreach ($model as $concreteModel) {
         $this->runCrudAction($concreteModel);
     }
 }
Exemple #2
0
 /**
  * 根据$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();
 }