think\model\Merge::save PHP Метод

save() публичный Метод

保存模型数据 以及关联数据
public save ( mixed $data = [], array $where = [], string $sequence = null ) : integer | false
$data mixed 数据
$where array 更新条件
$sequence string 自增序列名
Результат integer | false
    public function save($data = [], $where = [], $sequence = null)
    {
        if (!empty($data)) {
            // 数据自动验证
            if (!$this->validateData($data)) {
                return false;
            }
            // 数据对象赋值
            foreach ($data as $key => $value) {
                $this->setAttr($key, $value);
            }
            if (!empty($where)) {
                $this->isUpdate = true;
            }
        }
        // 数据自动完成
        $this->autoCompleteData($this->auto);
        // 自动写入更新时间
        if ($this->autoWriteTimestamp && $this->updateTime) {
            $this->setAttr($this->updateTime, null);
        }
        $db = $this->db();
        $db->startTrans();
        try {
            if ($this->isUpdate) {
                // 自动写入
                $this->autoCompleteData($this->update);
                if (false === $this->trigger('before_update', $this)) {
                    return false;
                }
                if (empty($where) && !empty($this->updateWhere)) {
                    $where = $this->updateWhere;
                }
                if (!empty($where)) {
                    $pk = $this->getPk();
                    if (isset($this->mapFields[$pk])) {
                        $pk = $this->mapFields[$pk];
                    }
                    if (isset($where[$pk])) {
                        unset($where[$pk]);
                    }
                }
                // 处理模型数据
                $data = $this->parseData($this->name, $this->data);
                // 写入主表数据
                $result = $db->strict(false)->where($where)->update($data);
                // 写入附表数据
                foreach ($this->relationModel as $key => $model) {
                    $name = is_int($key) ? $model : $key;
                    $table = is_int($key) ? $db->getTable($model) : $model;
                    // 处理关联模型数据
                    $data = $this->parseData($name, $this->data);
                    $query = clone $db;
                    if ($query->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data)) {
                        $result = 1;
                    }
                }
                // 清空change
                $this->change = [];
                // 新增回调
                $this->trigger('after_update', $this);
            } else {
                // 自动写入
                $this->autoCompleteData($this->insert);
                // 自动写入创建时间
                if ($this->autoWriteTimestamp && $this->createTime) {
                    $this->setAttr($this->createTime, null);
                }
                if (false === $this->trigger('before_insert', $this)) {
                    return false;
                }
                // 处理模型数据
                $data = $this->parseData($this->name, $this->data, true);
                // 写入主表数据
                $result = $db->name($this->name)->strict(false)->insert($data);
                if ($result) {
                    $insertId = $db->getLastInsID($sequence);
                    // 写入外键数据
                    $pk = $this->getPk();
                    if ($insertId) {
                        if (is_string($pk)) {
                            $this->data[$pk] = $insertId;
                            if ($this->fk == $pk) {
                                $this->change[] = $pk;
                            }
                        }
                        $this->data[$this->fk] = $insertId;
                    }
                    // 写入附表数据
                    $source = $this->data;
                    if ($insertId && is_string($pk) && isset($source[$pk]) && $this->fk != $pk) {
                        unset($source[$pk]);
                    }
                    foreach ($this->relationModel as $key => $model) {
                        $name = is_int($key) ? $model : $key;
                        $table = is_int($key) ? $db->getTable($model) : $model;
                        // 处理关联模型数据
                        $data = $this->parseData($name, $source, true);
                        $query = clone $db;
                        $query->table($table)->strict(false)->insert($data);
                    }
                }
                // 标记为更新
                $this->isUpdate = true;
                // 清空change
                $this->change = [];
                // 新增回调
                $this->trigger('after_insert', $this);
            }
            $db->commit();
            return $result;
        } catch (\Exception $e) {
            $db->rollback();
            throw $e;
        }
    }