保存（新增）当前关联数据对象
@access public
@param  mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
@param  boolean  $replace    是否自动识别更新和写入
@return Model|false
353402
public function save($data, $replace = true)
    {
        if ($data instanceof Model) {
            $data = $data->getData();
        }

        // 保存关联表数据
        $data[$this->foreignKey] = $this->parent->{$this->localKey};

        $model = new $this->model;

        return $model->replace($replace)->save($data) ? $model : false;
    }
150797
protected function insert(Data $data)
    {
        $this->__before('insert', $data);
        $data->validate();

        if (!is_array($id = $this->doInsert($data))) {
            return false;
        }

        $this->pack($id, $data);
        $this->__after('insert', $data);

        return true;
    }
1313728
public function updateNewRecord($data) {

        $emailMessage = $this->model->find($data['id']);

        $emailMessage->user_id             = $data['user_id'];
        $emailMessage->priority_id         = $data['priority_id'];


        if (isset($data['from_email_address'])) {
            $emailMessage->from_email_address  = $data['from_email_address'];
        }

        if (isset($data['from_name'])) {
            $emailMessage->from_name = $data['from_name'];
        }

        if (isset($data['to_email_address'])) {
            $emailMessage->to_email_address  = $data['to_email_address'];
        }

        if (isset($data['to_name'])) {
            $emailMessage->to_name = $data['to_name'];
        }

        $emailMessage->subject             = $data['subject'];
        $emailMessage->body                = $data['body'];
        $emailMessage->priority_id         = $data['priority_id'];
        $emailMessage->archived            = $data['archived'];
        $emailMessage->updated_at          = $data['updated_at'];
        $emailMessage->updated_by          = $data['updated_by'];

        return $emailMessage->save();
    }
814205
public function save($new = false, $validate=true,$validateFields=[])
    {
        $this->isNewRecord = $new;

        if($validate){
            if(!$this->validate($validateFields)){
                return false;
            }
        }
        if(!static::beforeSave()) return false;
        $db = X::app()->db->table(self::tableName());
        $tableFields = $db->getFields(self::tableName());
        foreach($this->_modelData as $k=>$v){
            //删除数据库没有的字段
            if(!in_array($k,$tableFields)) unset($this->_modelData[$k]);
        }
        if ($new) {
            return $db->add($this->_modelData);
        } else {
            $id = $this->{self::$pk};
            unset($this->_modelData[self::$pk]);
            return $db->update([self::$pk => $id], $this->_modelData);
        }
    }
787555
public static function save(string $filename, string $data): bool
    {
        return \file_put_contents($filename, $data) !== false;
    }
507581
public function insert(array $data, $options = [], $replace = false)
    {
        // 分析并处理数据
        $data = $this->parseData($data, $options);
        if (empty($data)) {
            return 0;
        }
        $fields = array_keys($data);
        $values = array_values($data);

        $sql = str_replace(
            ['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
            [
                $replace ? 'REPLACE' : 'INSERT',
                $this->parseTable($options['table'], $options),
                implode(' , ', $fields),
                implode(' , ', $values),
                $this->parseComment($options['comment']),
            ], $this->insertSql);

        return $sql;
    }
777050
public function save($data = null, $return = Db::RETURN_ISOK)
	{
		if(null === $data)
		{
			$data = $this->data;
		}
		$table = $this->getOptionTable();
		if(empty($this->fields) || $table !== $this->tableName())
		{
			$this->loadFields($fields, $fieldNames, $pk, $bitFields, $table);
		}
		else
		{
			$pk = $this->pk;
		}
		$pk = explode(',', $pk);
		$isEdit = true;
		foreach($pk as $tpk)
		{
			if(!array_key_exists($tpk, $data))
			{
				$isEdit = false;
				break;
			}
		}
		// 判断记录是否存在，来决定$isEdit的值
		if($isEdit)
		{
			$option = $this->operationOption;
			$isEdit = $this->wherePk($data,$table)->count() > 0;
			$this->operationOption = $option;
		}
		// 2个if不要合并！
		if($isEdit)
		{
			$result = $this->wherePk($data,$table)->edit($data,$return);
			if(Db::RETURN_INSERT_ID === $result)
			{
				return $pk[0];
			}
			else
			{
				return $result;
			}
		}
		else
		{
			return $this->add($data);
		}
	}
198708
public function replaceData(array $data, $filter = null)
	{
		$this->clearData();

		return $this->set($data, $filter);
	}
749520
public function update($data = null): IResult
    {
        $query = static::query($this);
        $query = $this->parseWhereId($query);
        if(null === $data)
        {
            $data = static::parseSaveData($this);
        }
        else if(!$data instanceof \ArrayAccess)
        {
            $data = new LazyArrayObject($data);
        }

        // 更新前
        $this->trigger(ModelEvents::BEFORE_UPDATE, [
            'model' => $this,
            'data'  => $data,
            'query' => $query,
        ], $this, \Imi\Model\Event\Param\BeforeUpdateEventParam::class);

        if(!isset($query->getOption()->where[0]))
        {
            throw new \RuntimeException('use Model->update(), primary key can not be null');
        }

        $result = $query->update($data);

        // 更新后
        $this->trigger(ModelEvents::AFTER_UPDATE, [
            'model' => $this,
            'data'  => $data,
            'result'=> $result,
        ], $this, \Imi\Model\Event\Param\AfterUpdateEventParam::class);

        // 子模型更新
        ModelRelationManager::updateModel($this);

        return $result;
    }
958257
public function add($data='',$options=array(),$replace=false) {
        if(empty($data)) {
            // 没有传递数据，获取当前数据对象的值
            if(!empty($this->data)) {
                $data           =   $this->data;
                // 重置数据
                $this->data     = array();
            }else{
                $this->error    = L('_DATA_TYPE_INVALID_');
                return false;
            }
        }
        // 分析表达式
        $options    =   $this->_parseOptions($options);
        // 数据处理
        $data       =   $this->_facade($data);
        if(false === $this->_before_insert($data,$options)) {
            return false;
        }
        // 写入数据到数据库
        $result = $this->db->insert($data,$options,$replace);
        if(false !== $result ) {
            $this->_after_insert($data,$options);
            if(isset($data[$this->getPk()])){
                return $data[$this->getPk()];
            }
        }
        return $result;
    }
6263
public function replace($table, $data) {

        $_fileds = '';
        $_values = '';
        $_T_fields = $this->getTableFields($table);
        foreach ( $data as $_key => $_val ) {

            //自动过滤掉不存在的字段
            if ( !in_array( $_key, $_T_fields ) ) continue;

            $_fileds .= ( $_fileds=='' ) ? "`{$_key}`" : ", `{$_key}`";
            if ( is_null($_val) ) {
                $_val = 'NULL';
            } else {
                $_val = "'{$_val}'";
            }
            $_values .= ( $_values=='' ) ? "{$_val}" : ",{$_val}";
        }

        if ( $_fileds != '' ) {
            $_query = "REPLACE INTO {$table}(" . $_fileds . ") VALUES(" . $_values . ")";
            if ( $this->execute($_query) != false ) {
                return true;
            }

        }
        return false;
    }
685771
public function save($file, $replace = true) {
        $header['Content-Type'] = $file['type'];
        $header['Content-MD5'] 	= $file['md5'];
        $header['Mkdir'] = 'true';
        $resource = fopen($file['tmp_name'], 'r');

        $save = $this->rootPath . $file['savepath'] . $file['savename'];
        $data = $this->request($save, 'PUT', $header, $resource);
        return false === $data ? false : true;
    }
344790
public function insertGetId(array $data, $replace = false, $sequence = null)
    {
        return $this->insert($data, $replace, true, $sequence);
    }
584907
public function insert($table, $data)
    {
		$_fileds = '';
		$_values = '';
		$_T_fields = $this->getTableFields($table);
		foreach ( $data as $_key => $_val ) {

			//自动过滤掉不存在的字段
			if ( !in_array( $_key, $_T_fields ) ) continue;

			$_fileds .= ( $_fileds=='' ) ? "`{$_key}`" : ", `{$_key}`" ;
            if ( is_null($_val) ) {
                $_val = 'NULL';
            } else {
                $_val = "'{$_val}'";
            }
			$_values .= ( $_values=='' ) ? "{$_val}" : ",{$_val}";

		}

		if ( $_fileds != '' ) {
			$_query = "INSERT INTO {$table}(" . $_fileds . ") VALUES(" . $_values . ")";

			if ( $this->execute( $_query ) != false ) {
                $last_insert_id = $this->link->lastInsertId();
                if ( $last_insert_id > 0 ) { //返回自增id
                    return $last_insert_id;
                } else {
                    return true;
                }
			}
		}
        return false;
    }
970961
public static function replace($table, $data)
    {
        $db = self::Using(self::$using_dbo_id);
        return $db->replace($table, $data);
    }
1299897
public function insertAll($dataSet,$options=array(),$replace=false) {
        $values  =  array();
        $this->model  =   $options['model'];
        if(!is_array($dataSet[0])) return false;
        $this->parseBind(!empty($options['bind'])?$options['bind']:array());
        $fields =   array_map(array($this,'parseKey'),array_keys($dataSet[0]));
        foreach ($dataSet as $data){
            $value   =  array();
            foreach ($data as $key=>$val){
                if(is_array($val) && 'exp' == $val[0]){
                    $value[]   =  $val[1];
                }elseif(is_null($val)){
                    $value[]   =   'NULL';
                }elseif(is_scalar($val)){
                    if(0===strpos($val,':') && in_array($val,array_keys($this->bind))){
                        $value[]   =   $this->parseValue($val);
                    }else{
                        $name       =   count($this->bind);
                        $value[]   =   ':'.$name;
                        $this->bindParam($name,$val);
                    }
                }
            }
            $values[]    = '('.implode(',', $value).')';
        }
        // 兼容数字传入方式
        $replace= (is_numeric($replace) && $replace>0)?true:$replace;
        $sql    =  (true===$replace?'REPLACE':'INSERT').' INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES '.implode(',',$values).$this->parseDuplicate($replace);
        $sql    .= $this->parseComment(!empty($options['comment'])?$options['comment']:'');
        return $this->execute($sql,!empty($options['fetch_sql']) ? true : false);
    }
905430
private function _insert_replace( $table, $data, $method ) {
		if ( ! isset( $this->table_defines[ $table ] ) ) {
			return false;
		}
		if ( $method !== 'insert' && $method !== 'replace' ) {
			return false;
		}
		if ( $method === 'replace' && ! isset( $data['id'] ) ) {
			return false;
		}

		/** @var \wpdb $wpdb */
		global $wpdb;
		$columns = $this->table_defines[ $table ]['columns'];

		$data = $this->set_update_params( $data, $method === 'insert', true, false );
		list ( $_data, $_format ) = $this->filter( $data, $columns );

		return $wpdb->$method( $this->get_table( $table ), $_data, $_format );
	}