创建记录.

@param array    $data 数据.
@param \Closure $cb   数据处理函数.

@return bool|int 成功返回true或主键值,失败返回false.
@throws
13871
protected final function insert($data, $cb = null) {
        if ($cb && $cb instanceof \Closure) {
            $data = $cb ($data, $this);
        }
        if ($data) {
            $this->filterFields($data);
            if (method_exists($this, 'validateNewData')) {
                if (isset($this->_formData)) {
                    $this->validateNewData($this->_formData);
                } else {
                    $this->validateNewData($data);
                }
            }
            $sql = new InsertSQL($data);
            $sql->into($this->table)->setDialect($this->dialect);
            if ($this->autoIncrement) {
                $sql->autoField($this->primaryKey);
                $rst = $sql->newId();
            } else {
                $rst = $sql->exec(true);
            }
            if ($rst) {
                return $rst;
            } else {
                $this->checkSQL($sql);

                return false;
            }
        } else {
            $this->errors = '数据为空.';

            return false;
        }
    }
106800
public static function destroy($data)
    {
        $model = new static();

        $query = $model->db();

        if (empty($data) && 0 !== $data) {
            return false;
        } elseif (is_array($data) && key($data) !== 0) {
            $query->where(self::parseWhere($data));
            $data = null;
        } elseif ($data instanceof \Closure) {
            $data($query);
            $data = null;
        }

        $resultSet = $query->select($data);

        if ($resultSet) {
            foreach ($resultSet as $data) {
                $data->delete();
            }
        }

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

        return $results;
    }
293703
final public function validate($data)
    {
        if ($this->_walk && (is_array($data) || is_object($data))) {
            $arr = (array)$data;

            foreach ($arr as $value) {
                if ($this->validate($value) ===  false) {
                    return false;
                }
            }
        } else {
            $context = $this->_chain->getContext();
            $context->data = $data;

            $result = $this->_chain->run('validate', $context);

            if ($result ===  false) {
                return false;
            }
        }

        return true;
    }
916227
public function update(array $data)
    {
        $where = [];
        $options = $this->parseExpress();
        $data    = array_merge($options['data'], $data);
        if (isset($options['cache']) && is_string($options['cache']['key'])) {
            $key = $options['cache']['key'];
        }
        $pk = $this->getPk();
        if (empty($options['where'])) {
            // 如果存在主键数据 则自动作为更新条件
            if (is_string($pk) && isset($data[$pk])) {
                $where[$pk] = $data[$pk];
                $key        = 'mongo:' . $options['table'] . '|' . $data[$pk];
                unset($data[$pk]);
            } elseif (is_array($pk)) {
                // 增加复合主键支持
                foreach ($pk as $field) {
                    if (isset($data[$field])) {
                        $where[$field] = $data[$field];
                    } else {
                        // 如果缺少复合主键数据则不执行
                        throw new Exception('miss complex primary data');
                    }
                    unset($data[$field]);
                }
            }
            if (!isset($where)) {
                // 如果没有任何更新条件则不执行
                throw new Exception('miss update condition');
            } else {
                $options['where']['$and'] = $where;
            }
        } elseif (!isset($key) && is_string($pk) && isset($options['where']['$and'][$pk])) {
            $key = $this->getCacheKey($options['where']['$and'][$pk], $options);
        }

        // 生成bulkWrite对象
        $bulk         = $this->builder->update($data, $options);
        $writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : null;
        $writeResult  = $this->execute($options['table'], $bulk, $writeConcern);

        $result = $writeResult->getModifiedCount();
        if ($result) {
            if (isset($where[$pk])) {
                $data[$pk] = $where[$pk];
            } elseif (is_string($pk) && isset($key) && strpos($key, '|')) {
                list($a, $val) = explode('|', $key);
                unset($a);
                $data[$pk]     = $val;
            }
            $options['data'] = $data;
            $this->trigger('after_update', $options);
        }
        return $result;
    }
817562
function trigger($type, $data)
    {
        /**
         * 异步，将事件压入队列
         */
        if (isset($this->config['async']) && $this->config['async'])
        {
            return $this->getQueueInstance()->push(array('type' => $type, 'data' => $data));
        }
        /**
         * 同步，直接在引发事件时处理
         */
        else
        {
            return $this->_execute($type, $data);
        }
    }
573902
public static function find(array $data, callable $callback = null, $keepKey = false, $offset = null, $limit = null)
    {
        $results = [];
        $i = 0;
        $c = 0;

        $callback = null === $callback ? 'is_null' : $callback;

        foreach ($data as $key => $value) {
            // If use global function, send only value as argument.
            if (is_string($callback)) {
                $r = $callback($value);
            } else {
                $r = $callback($value, $key);
            }

            if ($r) {
                if ($offset !== null && $offset > $i) {
                    continue;
                }

                if ($limit !== null && $c >= $limit) {
                    break;
                }

                $results[$key] = $value;
                $c++;
            }

            $i++;
        }

        return $keepKey ? $results : array_values($results);
    }
226723
public function verify(array $data)
    {
        if (is_callable($this->callback)) {
            return call_user_func($this->callback, $data);
        }

        return false;
    }
609685
protected function _moveNode(Closure $callback, array $data) {
        return $this->getRepository()->updateMany($data, $callback, [
            'before' => false,
            'after' => false
        ]);
    }
525076
public function copyFrom($data)
    {
        if (is_array($data) || $data instanceof \Traversable) {
            if ($this->getCount() > 0) {
                $this->clear();
            }
            if ($data instanceof CMap) {
                $data = $data->_data;
            }
            foreach ($data as $key => $value) {
                $this->add($key, $value);
            }
        } elseif ($data !== null) {
            throw new Exception('实例化列表参数必须是数组或可遍历的对象', 100100601);
        }
    }
1036918
public function convert($data)
    {
        foreach ($this->chain as $converter) {
            $result = $converter->convert($data);
            if ($result !== false) {
                return $result;
            }
        }
        
        return false;
    }
798777
public function update(array $data)
    {
        $this->compileData($data);
        $arguments      = $this->arguments;
        $sql            = 'UPDATE '.$this->getFrom().' SET '.$this->setString;
        //设置了条件
        if ($this->condition->count() > 0) {
            $whereStr       = (string)$this->condition->compile();
            $whereArguments = $this->condition->arguments();
            $sql            = $sql.' where '.$whereStr;
            $arguments      = ArrayLib::array_add($arguments, $whereArguments);
        }
        return $this->connection->executeUpdate($sql, $arguments);
    }
1322752
public function select($data = null)
    {
        if ($data instanceof Query) {
            return $data->select();
        } elseif ($data instanceof \Closure) {
            $data($this);
            $data = null;
        }

        $this->parseOptions();

        if (false === $data) {
            // 用于子查询 不查询只返回SQL
            $this->options['fetch_sql'] = true;
        } elseif (!is_null($data)) {
            // 主键条件分析
            $this->parsePkWhere($data);
        }

        $this->options['data'] = $data;

        $resultSet = $this->connection->select($this);

        if (!empty($this->options['fetch_sql'])) {
            return $resultSet;
        }

        // 数据列表读取后的处理
        if (!empty($this->model)) {
            // 生成模型对象
            $resultSet = $this->resultSetToModelCollection($resultSet);
        } else {
            $this->resultSet($resultSet);
        }

        // 返回结果处理
        if (!empty($this->options['fail']) && count($resultSet) == 0) {
            $this->throwNotFound($this->options);
        }

        return $resultSet;
    }
712869
protected function cacheLockVersion($data) {
        if($this->optimLock) {
            if(isset($data[$this->optimLock]) && isset($data[$this->getPk()])) {
                // 只有当存在乐观锁字段和主键有值的时候才记录乐观锁
                $_SESSION[$this->name.'_'.$data[$this->getPk()].'_lock_version']    =   $data[$this->optimLock];
            }
        }
    }
1148972
public function delete($data = null)
    {
        $this->parseOptions();

        if (!is_null($data) && true !== $data) {
            // AR模式分析主键条件
            $this->parsePkWhere($data);
        }

        if (!empty($this->options['soft_delete'])) {
            // 软删除
            list($field, $condition) = $this->options['soft_delete'];
            unset($this->options['soft_delete']);
            $this->options['data'] = [$field => $condition];

            return $this->connection->update($this);
        }

        $this->options['data'] = $data;

        return $this->connection->delete($this);
    }
624726
public function sendFrame($data, $type = null, $cb = null)
    {
        if (!$this->handshaked) {
            return false;
        }

        if ($this->finished && $type !== 'CONNCLOSE') {
            return false;
        }

        /*if (in_array($type, ['STRING', 'BINARY']) && ($this->outgoingCompression > 0) && in_array('deflate-frame', $this->extensions)) {
            //$data = gzcompress($data, $this->outgoingCompression);
            //$rsv1 = 1;
        }*/

        $fin = 1;
        $rsv1 = 0;
        $rsv2 = 0;
        $rsv3 = 0;
        $this->write(
            chr(
                bindec(
                    $fin . $rsv1 . $rsv2 . $rsv3
                    . str_pad(
                        decbin(
                            $this->getFrameType($type)
                        ),
                        4,
                        '0',
                        STR_PAD_LEFT
                    )
                )
            )
        );
        $dataLength = mb_orig_strlen($data);
        $isMasked = false;
        $isMaskedInt = $isMasked ? 128 : 0;
        if ($dataLength <= 125) {
            $this->write(chr($dataLength + $isMaskedInt));
        } elseif ($dataLength <= 65535) {
            $this->write(chr(126 + $isMaskedInt) . // 126 + 128
                chr($dataLength >> 8) .
                chr($dataLength & 0xFF));
        } else {
            $this->write(chr(127 + $isMaskedInt) . // 127 + 128
                chr($dataLength >> 56) .
                chr($dataLength >> 48) .
                chr($dataLength >> 40) .
                chr($dataLength >> 32) .
                chr($dataLength >> 24) .
                chr($dataLength >> 16) .
                chr($dataLength >> 8) .
                chr($dataLength & 0xFF));
        }
        if ($isMasked) {
            $mask = chr(mt_rand(0, 0xFF)) .
                chr(mt_rand(0, 0xFF)) .
                chr(mt_rand(0, 0xFF)) .
                chr(mt_rand(0, 0xFF));
            $this->write($mask . $this->mask($data, $mask));
        } else {
            $this->write($data);
        }
        if ($cb !== null) {
            $this->onWriteOnce($cb);
        }
        return true;
    }
1010349
static function parseHeader($data)
    {
        $header = array();
        $header[0] = array();
        $meta = &$header[0];
        $parts = explode("\r\n\r\n", $data, 2);

        // parts[0] = HTTP头;
        // parts[1] = HTTP主体，GET请求没有body
        $headerLines = explode("\r\n", $parts[0]);

        // HTTP协议头,方法，路径，协议[RFC-2616 5.1]
        list($meta['method'], $meta['uri'], $meta['protocol']) = explode(' ', $headerLines[0], 3);

        //错误的HTTP请求
        if (empty($meta['method']) or empty($meta['uri']) or empty($meta['protocol']))
        {
            return false;
        }
        unset($headerLines[0]);
        //解析Header
        $header = array_merge($header, self::parseHeaderLine($headerLines));
        return $header;
    }