更新一条数据。

@param  Illuminate\Database\Eloquent\Model  $model
@param  array  $data
@param  \Closure  $callback
@return void | \Larawos\Illuminate\GeneralException
763538
public function update(Model $model, array $data, $callback = null)
    {
        $model = $model->fill($data);

        DB::transaction(function () use ($model, $data, $callback) {
            if ($model->save()) {

                if ($callback instanceof Closure) {
                    $callback($model, $data);
                }

                return true;
            }

            throw new GeneralException($model, '更新时出现问题，请再试一次。');
        });

        return $model;
    }
396425
public function restore($id, $callback = null)
    {
        $model = $this->query()->withTrashed()->find($id);

        if (is_null($model->deleted_at)) {
            throw new GeneralException('该数据未删除，因此无法恢复。');
        }

        DB::transaction(function () use ($model, $callback) {
            if ($model->restore()) {
                if ($callback instanceof Closure) {
                    $callback($model);
                }

                return true;
            }

            throw new GeneralException($model, '还原时出现问题，请再试一次。');
        });

        return $model;
    }
447772
public function persist($model, $errors = array(), $data = array()) {
		if (is_a($model, 'Model')) {
			$errors = $model->validationErrors;
			$data = $model->data;
			$model = $model->alias;
		}

		return $this->Session->write("$this->sessionKey.$model", compact('data', 'errors'));
	}
1018439
private function fillModel(AbstractModel $model, array $data)
    {
        if (!$data) {
            return $model;
        }

        $reflection = new \ReflectionClass($model);
        foreach($reflection->getProperties() as $property) {
            $property->setAccessible(true);
            $property->setValue($model, $data[$property->getName()]);
            $property->setAccessible(false);
        }

        return $model;
    }
176277
public function send($view, array $data, Closure $callback)
    {
        return $this->mailer->send($view, $data, $callback);
    }
171172
public function each($data = [], Closure $callback)
    {
        $factory = $this->getFactory();
        $results = array();
        foreach ($data as $item) {
            $callback($factory, $item);
            $results[] = $factory->getResults();
        }

        return $results;
    }
416963
public function patch($model, $data)
    {
        try {
            $resource = $model->update($data);
        } catch (\Exception $e) {
            // Check for QueryException - if so, we may want to display a more meaningful message, or help with
            // development debugging
            if ($e instanceof QueryException) {
                if (stristr($e->getMessage(), 'duplicate')) {
                    throw new ConflictHttpException('Duplicate entry for ' . $this->model);
                } elseif (Config::get('api.debug') === true) {
                    throw $e;
                }
            }

            // Default HTTP exception to use for storage errors
            $errorMessage = 'Unexpected error trying to store this resource.';

            if (Config::get('api.debug') === true) {
                $errorMessage .= ' ' . $e->getMessage();
            }

            throw new UnprocessableEntityHttpException($errorMessage);
        }

        return $resource;
    }
1180007
private function update($model)
    {
        $attrs = $this->getMutableAttributes($model);
        $values = array_values($attrs);
        $values[] = $model->{$this->key};
        $this->db
            ->connect()
            ->update($this->table, array_keys($attrs))
            ->where($this->key . ' = ?')
            ->execute($values);
    }
1126517
public function populateJoinedRelations($model, array $row)
    {
        foreach ($row as $key => $value) {
            if (empty($this->join) || !is_array($value) || $model->hasAttribute($key)) {
                continue;
            }
            foreach ($this->join as $join) {
                $keys = array_keys($join);
                $name = array_shift($keys);
                $closure = array_shift($join);

                if (is_int($name)) {
                    $name = $closure;
                    $closure = null;
                }
                if ($name !== $key) {
                    continue;
                }
                if ($model->isRelationPopulated($name)) {
                    continue 2;
                }
                $records = [];
                $relation = $model->getRelation($name);
                $relationClass = $relation->modelClass;
                if ($closure !== null) {
                    call_user_func($closure, $relation);
                }
                $relation->prepare();

                if ($relation->multiple) {
                    foreach ($value as $item) {
                        $relatedModel = $relationClass::instantiate($item);
                        $relatedModelClass = get_class($relatedModel);
                        $relatedModelClass::populateRecord($relatedModel, $item);
                        $relation->populateJoinedRelations($relatedModel, $item);
                        $relation->addInverseRelation($relatedModel, $model);
                        $relatedModel->trigger(BaseActiveRecord::EVENT_AFTER_FIND);
                        if ($relation->indexBy !== null) {
                            $index = is_string($relation->indexBy)
                                ? $relatedModel[$relation->indexBy]
                                : call_user_func($relation->indexBy, $relatedModel);
                            $records[$index] = $relatedModel;
                        } else {
                            $records[] = $relatedModel;
                        }
                    }
                } else {
                    $relatedModel = $relationClass::instantiate($value);
                    $relatedModelClass = get_class($relatedModel);
                    $relatedModelClass::populateRecord($relatedModel, $value);
                    $relation->populateJoinedRelations($relatedModel, $value);
                    $relation->addInverseRelation($relatedModel, $model);
                    $relatedModel->trigger(BaseActiveRecord::EVENT_AFTER_FIND);
                    $records = $relatedModel;
                }

                $model->populateRelation($name, $records);
            }
        }
    }
1373520
public function save($model, $id, $data)
    {
        if (is_null($id)) { // insert
            $model->data = $this->sendCollectionRequest($model, $this->insert_mode, $data);
            $model->id = $model->data ? $model->data[$model->id_field] : null;
        } else { // update
            $model->data = $this->sendItemRequest($model, $id, $this->update_mode, $data);
            $model->id = $model->data ? $model->data[$model->id_field] : null;
        }

        return $model->id;
    }
1107932
public function beforeInsert($model, &$data)
    {
        if ($this->weak) {
            return;
        }

        // The value for the master_field is set, so we are going to use existing record anyway
        if ($model->hasField($this->master_field) && $model[$this->master_field]) {
            return;
        }

        $insert = $this->dsql();
        $insert->mode('insert');
        $insert->set($model->persistence->typecastSaveRow($model, $this->save_buffer));
        $this->save_buffer = [];
        $insert->set($this->foreign_field, null);
        $insert->insert();
        $this->id = $this->owner->lastInsertID();

        if ($this->join) {
            $this->join->set($this->master_field, $this->id);
        } else {
            $data[$this->master_field] = $this->id;
        }
    }
517026
protected function doAddMultiples(Skill $model, $data) {
		$errors = [];
		foreach ($data as $entry) {
			if (!isset($entry['id'])) {
				$errors[] = 'Missing id for Skill';
			} else {
				$related = SkillQuery::create()->findOneById($entry['id']);
				$model->addMultiple($related);
			}
		}

		if (count($errors) > 0) {
			return new ErrorsException($errors);
		}
	}
1254022
public function afterSave($model, $data) {
        // Check if the 'published column' is filled...
        if (empty($data[self::PUBLISHED_COLUMN])) {
            return;
        }

        // ...and that it's in the future
        $publishTime = strtotime($data[self::PUBLISHED_COLUMN]);
        if ($publishTime <= time()) {
            return;
        }

        $tags = array(get_class($model));
        $tags = array_merge($tags, $model->getBindableModels());
        $tags = array_unique($tags);
        Garp_Cache_Manager::scheduleClear($publishTime, $tags);
    }
209702
public function setModel(\atk4\data\Model $model, $fields = null)
    {
        parent::setModel($model);

        if ($fields === false) {
            return $model;
        }

        if ($fields === null) {
            $fields = $this->getModelFields($model);
        }

        // prepare array of fields - check if fields are editable or read_only/disabled
        $add_fields = [];
        foreach ($fields as $field) {
            $f = $model->getElement($field);

            if ($f->isEditable()) {
                $add_fields[] = [$f->short_name];
            } elseif ($f->isVisible()) {
                $add_fields[] = [$f->short_name, ['readonly'=>true]];
            }
        }

        if (is_array($add_fields)) {
            foreach ($add_fields as $field) {
                call_user_func_array([$this, 'addField'], $field);
            }
        } else {
            throw new Exception(['Incorrect value for $fields', 'fields' => $add_fields]);
        }

        return $model;
    }
215108
protected function doUpdateSkills(Reference $model, $data) {
		// remove all relationships before
		SkillReferenceQuery::create()->filterByReference($model)->delete();

		// add them
		$errors = [];
		foreach ($data as $entry) {
			if (!isset($entry['id'])) {
				$errors[] = 'Missing id for Skill';
			} else {
				$related = SkillQuery::create()->findOneById($entry['id']);
				$model->addSkill($related);
			}
		}

		if (count($errors) > 0) {
			throw new ErrorsException($errors);
		}
	}
593528
public function update($id, array $data)
    {
        return $this->model->where($this->model->getKeyName(), $id)->update($data);
    }
559954
protected function doRemoveSkills(Group $model, $data) {
		$errors = [];
		foreach ($data as $entry) {
			if (!isset($entry['id'])) {
				$errors[] = 'Missing id for Skill';
			} else {
				$related = SkillQuery::create()->findOneById($entry['id']);
				$model->removeSkill($related);
			}
		}

		if (count($errors) > 0) {
			return new ErrorsException($errors);
		}
	}