Example #1
0
 /**
  * Custom method for HiResource
  *
  * @param $action
  * @param array $options
  * @param bool $bulk
  * @return array
  * @throws HiResException
  */
 public static function perform($action, $options = [], $bulk = false)
 {
     $action = $bulk == true ? static::index() . $action : static::modelName() . $action;
     $result = static::getDb()->createCommand()->perform($action, $options);
     if (Err::isError($result)) {
         throw new HiResException('Hiresource method: ' . $action, Err::getError($result));
     }
     return $result;
 }
Example #2
0
 public function update($runValidation = true, $attributes = null, $options = [])
 {
     if (!$attributes) {
         $attributes = $this->attributes ?: $this->first->activeAttributes();
     }
     if ($runValidation && !$this->validate($attributes)) {
         return false;
     }
     if (!$this->beforeSave()) {
         return false;
     }
     $data = $this->collectData($attributes, $options);
     $command = $this->first->getScenarioCommand('update', true);
     $results = $this->first->getDb()->createCommand()->perform($command, $data);
     if ($results === false || Err::isError($results)) {
         throw new HiResException(Err::getError($results), Json::encode($results));
     }
     foreach ($this->models as $key => $model) {
         $changedAttributes = [];
         $values =& $data[$key];
         foreach ($values as $name => $value) {
             /* @var $model ActiveRecord */
             $changedAttributes[$name] = $model->getOldAttribute($name);
             $model->setOldAttribute($name, $value);
         }
         $model->afterSave(false, $changedAttributes);
     }
     $this->afterSave();
     return true;
 }