Db::insertAll PHP Method

insertAll() public method

+---------------------------------------------------------- 插入记录 +---------------------------------------------------------- +----------------------------------------------------------
public insertAll ( mixed $datas, array $options = [] ) : false | integer
$datas mixed 数据
$options array 参数表达式 +----------------------------------------------------------
return false | integer | integer +----------------------------------------------------------
    public function insertAll($datas, $options = array())
    {
        if (!is_array($datas[0])) {
            return false;
        }
        $fields = array_keys($datas[0]);
        array_walk($fields, array($this, 'addSpecialChar'));
        $values = array();
        foreach ($datas as $data) {
            $value = array();
            foreach ($data as $key => $val) {
                $val = $this->parseValue($val);
                if (is_scalar($val)) {
                    // 过滤非标量数据
                    $value[] = $val;
                }
            }
            $values[] = '(' . implode(',', $value) . ')';
        }
        $sql = 'INSERT INTO ' . $this->parseTable($options['table']) . ' (' . implode(',', $fields) . ') VALUES ' . implode(',', $values);
        return $this->execute($sql);
    }

Usage Example

 public function save_array($param_array)
 {
     return Db::insertAll(self::TABLE_NAME, $param_array);
 }