diff --git a/src/Model.php b/src/Model.php index 45ec53eb..034ca7ad 100644 --- a/src/Model.php +++ b/src/Model.php @@ -239,7 +239,7 @@ public function __construct(array $data = []) if (empty($this->name)) { // 当前模型名 - $name = str_replace('\\', '/', static::class); + $name = str_replace('\\', '/', static::class); $this->name = basename($name); } @@ -472,9 +472,9 @@ public function replace(bool $replace = true) public function refresh(bool $relation = false) { if ($this->exists) { - $this->data = $this->db()->find($this->getKey())->getData(); + $this->data = $this->db()->find($this->getKey())->getData(); $this->origin = $this->data; - $this->get = []; + $this->get = []; if ($relation) { $this->relation = []; @@ -561,8 +561,8 @@ public function save(array $data = [], string $sequence = null): bool $this->trigger('AfterWrite'); // 重新记录原始数据 - $this->origin = $this->data; - $this->get = []; + $this->origin = $this->data; + $this->get = []; $this->lazySave = false; return true; @@ -631,7 +631,7 @@ protected function updateData(): bool if ($this->autoWriteTimestamp && $this->updateTime) { // 自动写入更新时间 - $data[$this->updateTime] = $this->autoWriteTimestamp(); + $data[$this->updateTime] = $this->autoWriteTimestamp(); $this->data[$this->updateTime] = $data[$this->updateTime]; } @@ -655,7 +655,7 @@ protected function updateData(): bool $db->transaction(function () use ($data, $allowFields, $db) { $this->key = null; - $where = $this->getWhere(); + $where = $this->getWhere(); $result = $db->where($where) ->strict(false) @@ -696,12 +696,12 @@ protected function insertData(string $sequence = null): bool // 时间戳自动写入 if ($this->autoWriteTimestamp) { if ($this->createTime && !isset($data[$this->createTime])) { - $data[$this->createTime] = $this->autoWriteTimestamp(); + $data[$this->createTime] = $this->autoWriteTimestamp(); $this->data[$this->createTime] = $data[$this->createTime]; } if ($this->updateTime && !isset($data[$this->updateTime])) { - $data[$this->updateTime] = $this->autoWriteTimestamp(); + $data[$this->updateTime] = $this->autoWriteTimestamp(); $this->data[$this->updateTime] = $data[$this->updateTime]; } } @@ -754,7 +754,7 @@ public function getWhere() $pk = $this->getPk(); if (is_string($pk) && isset($this->origin[$pk])) { - $where = [[$pk, '=', $this->origin[$pk]]]; + $where = [[$pk, '=', $this->origin[$pk]]]; $this->key = $this->origin[$pk]; } elseif (is_array($pk)) { foreach ($pk as $field) { @@ -841,7 +841,7 @@ public function delete(): bool $this->trigger('AfterDelete'); - $this->exists = false; + $this->exists = false; $this->lazySave = false; return true; diff --git a/src/Paginator.php b/src/Paginator.php index eace6ba8..f0f85b3d 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -76,8 +76,8 @@ abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, J */ protected $options = [ 'var_page' => 'page', - 'path' => '/', - 'query' => [], + 'path' => '/', + 'query' => [], 'fragment' => '', ]; @@ -104,7 +104,7 @@ public function __construct($items, int $listRows, int $currentPage = 1, int $to $this->options['path'] = '/' != $this->options['path'] ? rtrim($this->options['path'], '/') : $this->options['path']; - $this->simple = $simple; + $this->simple = $simple; $this->listRows = $listRows; if (!$items instanceof Collection) { @@ -113,13 +113,13 @@ public function __construct($items, int $listRows, int $currentPage = 1, int $to if ($simple) { $this->currentPage = $this->setCurrentPage($currentPage); - $this->hasMore = count($items) > ($this->listRows); - $items = $items->slice(0, $this->listRows); + $this->hasMore = count($items) > ($this->listRows); + $items = $items->slice(0, $this->listRows); } else { - $this->total = $total; - $this->lastPage = (int) ceil($total / $listRows); + $this->total = $total; + $this->lastPage = (int) ceil($total / $listRows); $this->currentPage = $this->setCurrentPage($currentPage); - $this->hasMore = $this->currentPage < $this->lastPage; + $this->hasMore = $this->currentPage < $this->lastPage; } $this->items = $items; } @@ -172,10 +172,10 @@ protected function url(int $page): string if (strpos($this->options['path'], '[PAGE]') === false) { $parameters = [$this->options['var_page'] => $page]; - $path = $this->options['path']; + $path = $this->options['path']; } else { $parameters = []; - $path = str_replace('[PAGE]', (string) $page, $this->options['path']); + $path = str_replace('[PAGE]', (string) $page, $this->options['path']); } if (count($this->options['query']) > 0) { @@ -505,11 +505,11 @@ public function toArray(): array } return [ - 'total' => $total, - 'per_page' => $this->listRows(), + 'total' => $total, + 'per_page' => $this->listRows(), 'current_page' => $this->currentPage(), - 'last_page' => $this->lastPage, - 'data' => $this->items->toArray(), + 'last_page' => $this->lastPage, + 'data' => $this->items->toArray(), ]; } diff --git a/src/db/BaseQuery.php b/src/db/BaseQuery.php index 60aa21e4..ab2102fb 100644 --- a/src/db/BaseQuery.php +++ b/src/db/BaseQuery.php @@ -343,7 +343,7 @@ public function field($field) if (true === $field) { // 获取全部字段 $fields = $this->getTableFields(); - $field = $fields ?: ['*']; + $field = $fields ?: ['*']; } if (isset($this->options['field'])) { @@ -373,7 +373,7 @@ public function withoutField($field) // 字段排除 $fields = $this->getTableFields(); - $field = $fields ? array_diff($fields, $field) : $field; + $field = $fields ? array_diff($fields, $field) : $field; if (isset($this->options['field'])) { $field = array_merge((array) $this->options['field'], $field); @@ -406,7 +406,7 @@ public function tableField($field, string $tableName, string $prefix = '', strin if (true === $field) { // 获取全部字段 $fields = $this->getTableFields($tableName); - $field = $fields ?: ['*']; + $field = $fields ?: ['*']; } // 添加统一的前缀 @@ -452,7 +452,7 @@ public function removeOption(string $option = '') { if ('' === $option) { $this->options = []; - $this->bind = []; + $this->bind = []; } elseif (isset($this->options[$option])) { unset($this->options[$option]); } @@ -502,13 +502,13 @@ public function table($table) } elseif (false === strpos($table, ',')) { if (strpos($table, ' ')) { [$item, $alias] = explode(' ', $table); - $table = []; + $table = []; $this->alias([$item => $alias]); $table[$item] = $alias; } } else { $tables = explode(',', $table); - $table = []; + $table = []; foreach ($tables as $item) { $item = trim($item); @@ -523,7 +523,7 @@ public function table($table) } } elseif (is_array($table)) { $tables = $table; - $table = []; + $table = []; foreach ($tables as $key => $val) { if (is_numeric($key)) { @@ -600,22 +600,22 @@ public function order($field, string $order = '') public function paginate($listRows = null, $simple = false): Paginator { if (is_int($simple)) { - $total = $simple; + $total = $simple; $simple = false; } $defaultConfig = [ - 'query' => [], //url额外参数 - 'fragment' => '', //url锚点 - 'var_page' => 'page', //分页变量 + 'query' => [], //url额外参数 + 'fragment' => '', //url锚点 + 'var_page' => 'page', //分页变量 'list_rows' => 15, //每页数量 ]; if (is_array($listRows)) { - $config = array_merge($defaultConfig, $listRows); + $config = array_merge($defaultConfig, $listRows); $listRows = intval($config['list_rows']); } else { - $config = $defaultConfig; + $config = $defaultConfig; $listRows = intval($listRows ?: $config['list_rows']); } @@ -630,7 +630,7 @@ public function paginate($listRows = null, $simple = false): Paginator unset($this->options['order'], $this->options['cache'], $this->options['limit'], $this->options['page'], $this->options['field']); - $bind = $this->bind; + $bind = $this->bind; $total = $this->count(); if ($total > 0) { $results = $this->options($options)->bind($bind)->page($page, $listRows)->select(); @@ -643,7 +643,7 @@ public function paginate($listRows = null, $simple = false): Paginator } } elseif ($simple) { $results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select(); - $total = null; + $total = null; } else { $results = $this->page($page, $listRows)->select(); } @@ -666,20 +666,20 @@ public function paginate($listRows = null, $simple = false): Paginator public function paginateX($listRows = null, string $key = null, string $sort = null): Paginator { $defaultConfig = [ - 'query' => [], //url额外参数 - 'fragment' => '', //url锚点 - 'var_page' => 'page', //分页变量 + 'query' => [], //url额外参数 + 'fragment' => '', //url锚点 + 'var_page' => 'page', //分页变量 'list_rows' => 15, //每页数量 ]; - $config = is_array($listRows) ? array_merge($defaultConfig, $listRows) : $defaultConfig; + $config = is_array($listRows) ? array_merge($defaultConfig, $listRows) : $defaultConfig; $listRows = is_int($listRows) ? $listRows : (int) $config['list_rows']; - $page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']); - $page = $page < 1 ? 1 : $page; + $page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']); + $page = $page < 1 ? 1 : $page; $config['path'] = $config['path'] ?? Paginator::getCurrentPath(); - $key = $key ?: $this->getPk(); + $key = $key ?: $this->getPk(); $options = $this->getOptions(); if (is_null($sort)) { @@ -759,7 +759,7 @@ public function more(int $limit, $lastId = null, string $key = null, string $sor $result->first(); return [ - 'data' => $result, + 'data' => $result, 'lastId' => $last ? $last[$key] : null, ]; } @@ -780,10 +780,10 @@ public function cache($key = true, $expire = null, $tag = null) if ($key instanceof \DateTimeInterface || $key instanceof \DateInterval || (is_int($key) && is_null($expire))) { $expire = $key; - $key = true; + $key = true; } - $this->options['cache'] = [$key, $expire, $tag ?: $this->getTable()]; + $this->options['cache'] = [$key, $expire, $tag ?: $this->getTable()]; return $this; } @@ -898,7 +898,7 @@ public function sequence(string $sequence = null) */ public function json(array $json = [], bool $assoc = false) { - $this->options['json'] = $json; + $this->options['json'] = $json; $this->options['json_assoc'] = $assoc; return $this; @@ -1254,10 +1254,10 @@ public function parseOptions(): array if (isset($options['page'])) { // 根据页数计算limit [$page, $listRows] = $options['page']; - $page = $page > 0 ? $page : 1; - $listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20); - $offset = $listRows * ($page - 1); - $options['limit'] = $offset . ',' . $listRows; + $page = $page > 0 ? $page : 1; + $listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20); + $offset = $listRows * ($page - 1); + $options['limit'] = $offset . ',' . $listRows; } $this->options = $options; @@ -1274,7 +1274,7 @@ public function parseOptions(): array */ public function parseUpdateData(&$data): bool { - $pk = $this->getPk(); + $pk = $this->getPk(); $isUpdate = false; // 如果存在主键数据 则自动作为更新条件 if (is_string($pk) && isset($data[$pk])) { diff --git a/src/db/Builder.php b/src/db/Builder.php index 2bde77c3..bd718fcf 100644 --- a/src/db/Builder.php +++ b/src/db/Builder.php @@ -38,16 +38,16 @@ abstract class Builder * @var array */ protected $parser = [ - 'parseCompare' => ['=', '<>', '>', '>=', '<', '<='], - 'parseLike' => ['LIKE', 'NOT LIKE'], - 'parseBetween' => ['NOT BETWEEN', 'BETWEEN'], - 'parseIn' => ['NOT IN', 'IN'], - 'parseExp' => ['EXP'], - 'parseNull' => ['NOT NULL', 'NULL'], + 'parseCompare' => ['=', '<>', '>', '>=', '<', '<='], + 'parseLike' => ['LIKE', 'NOT LIKE'], + 'parseBetween' => ['NOT BETWEEN', 'BETWEEN'], + 'parseIn' => ['NOT IN', 'IN'], + 'parseExp' => ['EXP'], + 'parseNull' => ['NOT NULL', 'NULL'], 'parseBetweenTime' => ['BETWEEN TIME', 'NOT BETWEEN TIME'], - 'parseTime' => ['< TIME', '> TIME', '<= TIME', '>= TIME'], - 'parseExists' => ['NOT EXISTS', 'EXISTS'], - 'parseColumn' => ['COLUMN'], + 'parseTime' => ['< TIME', '> TIME', '<= TIME', '>= TIME'], + 'parseExists' => ['NOT EXISTS', 'EXISTS'], + 'parseColumn' => ['COLUMN'], ]; /** @@ -156,8 +156,8 @@ protected function parseData(Query $query, array $data = [], array $fields = [], } if (false !== strpos($key, '->')) { - [$key, $name] = explode('->', $key, 2); - $item = $this->parseKey($query, $key); + [$key, $name] = explode('->', $key, 2); + $item = $this->parseKey($query, $key); $result[$item . '->' . $name] = 'json_set(' . $item . ', \'$.' . $name . '\', ' . $this->parseDataBind($query, $key . '->' . $name, $val, $bind) . ')'; } elseif (false === strpos($key, '.') && !in_array($key, $fields, true)) { @@ -273,7 +273,7 @@ protected function parseField(Query $query, $fields): string */ protected function parseTable(Query $query, $tables): string { - $item = []; + $item = []; $options = $query->getOptions(); foreach ((array) $tables as $key => $table) { @@ -300,14 +300,14 @@ protected function parseTable(Query $query, $tables): string */ protected function parseWhere(Query $query, array $where): string { - $options = $query->getOptions(); + $options = $query->getOptions(); $whereStr = $this->buildWhere($query, $where); if (!empty($options['soft_delete'])) { // 附加软删除条件 [$field, $condition] = $options['soft_delete']; - $binds = $query->getFieldsBindType(); + $binds = $query->getFieldsBindType(); $whereStr = $whereStr ? '( ' . $whereStr . ' ) AND ' : ''; $whereStr = $whereStr . $this->parseWhereItem($query, $field, $condition, $binds); } @@ -387,7 +387,7 @@ protected function parseWhereLogic(Query $query, string $logic, array $val, arra $where[] = $this->parseFieldsAnd($query, $value, $field, $logic, $binds); } else { // 对字段使用表达式查询 - $field = is_string($field) ? $field : ''; + $field = is_string($field) ? $field : ''; $where[] = ' ' . $logic . ' ' . $this->parseWhereItem($query, $field, $value, $binds); } } @@ -523,7 +523,7 @@ protected function parseWhereItem(Query $query, $field, array $val, array $binds if (is_scalar($value) && !in_array($exp, ['EXP', 'NOT NULL', 'NULL', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']) && strpos($exp, 'TIME') === false) { if (is_string($value) && 0 === strpos($value, ':') && $query->isBind(substr($value, 1))) { } else { - $name = $query->bindValue($value, $bindType); + $name = $query->bindValue($value, $bindType); $value = ':' . $name; } } @@ -556,7 +556,7 @@ protected function parseLike(Query $query, string $key, string $exp, $value, $fi if (is_array($value)) { $array = []; foreach ($value as $item) { - $name = $query->bindValue($item, PDO::PARAM_STR); + $name = $query->bindValue($item, PDO::PARAM_STR); $array[] = $key . ' ' . $exp . ' :' . $name; } @@ -770,8 +770,8 @@ protected function parseIn(Query $query, string $key, string $exp, $value, $fiel if ($query->isAutoBind()) { $array = []; foreach ($value as $v) { - $name = $query->bindValue($v, $bindType); - $array[] = ':' . $name; + $name = $query->bindValue($v, $bindType); + $array[] = ':' . $name; } $value = implode(',', $array); } elseif (PDO::PARAM_STR == $bindType) { @@ -918,8 +918,8 @@ protected function parseOrder(Query $query, array $order): string } if (preg_match('/^[\w\.]+$/', $key)) { - $sort = strtoupper($sort); - $sort = in_array($sort, ['ASC', 'DESC'], true) ? ' ' . $sort : ''; + $sort = strtoupper($sort); + $sort = in_array($sort, ['ASC', 'DESC'], true) ? ' ' . $sort : ''; $array[] = $this->parseKey($query, $key, true) . $sort; } else { throw new Exception('order express error:' . $key); @@ -939,7 +939,7 @@ protected function parseOrder(Query $query, array $order): string */ protected function parseRaw(Query $query, Raw $raw): string { - $sql = $raw->getValue(); + $sql = $raw->getValue(); $bind = $raw->getBind(); if ($bind) { @@ -1251,7 +1251,7 @@ public function insertAllByKeys(Query $query, array $keys, array $datas): string $options = $query->getOptions(); // 获取绑定信息 - $bind = $query->getFieldsBindType(); + $bind = $query->getFieldsBindType(); $fields = []; $values = []; diff --git a/src/db/Connection.php b/src/db/Connection.php index 015cf441..1af80c4c 100644 --- a/src/db/Connection.php +++ b/src/db/Connection.php @@ -253,7 +253,7 @@ protected function trigger(string $sql = '', bool $master = false): void } $runtime = number_format((microtime(true) - $this->queryStartTime), 6); - $sql = $sql ?: $this->getLastsql(); + $sql = $sql ?: $this->getLastsql(); if (empty($this->config['deploy'])) { $master = null; diff --git a/src/db/Fetch.php b/src/db/Fetch.php index a997a859..f1916c77 100644 --- a/src/db/Fetch.php +++ b/src/db/Fetch.php @@ -45,9 +45,9 @@ class Fetch */ public function __construct(Query $query) { - $this->query = $query; + $this->query = $query; $this->connection = $query->getConnection(); - $this->builder = $this->connection->getBuilder(); + $this->builder = $this->connection->getBuilder(); } /** @@ -207,10 +207,10 @@ public function insertAll(array $dataSet = [], int $limit = null): string } if ($limit) { - $array = array_chunk($dataSet, $limit, true); + $array = array_chunk($dataSet, $limit, true); $fetchSql = []; foreach ($array as $item) { - $sql = $this->builder->insertAll($this->query, $item); + $sql = $this->builder->insertAll($this->query, $item); $bind = $this->query->getBind(); $fetchSql[] = $this->connection->getRealSql($sql, $bind); @@ -422,7 +422,7 @@ public function count(string $field = '*'): string if (!empty($options['group'])) { // 支持GROUP $subSql = $this->query->field('count(' . $field . ') AS think_count')->buildSql(); - $query = $this->query->newQuery()->table([$subSql => '_group_count_']); + $query = $this->query->newQuery()->table([$subSql => '_group_count_']); return $query->fetchsql()->aggregate('COUNT', '*'); } else { diff --git a/src/db/Mongo.php b/src/db/Mongo.php index cf6e9c4c..35cfd219 100644 --- a/src/db/Mongo.php +++ b/src/db/Mongo.php @@ -113,7 +113,7 @@ public function count(string $field = null): int public function aggregate(string $aggregate, $field, bool $force = false) { $result = $this->cmd('aggregate', [strtolower($aggregate), $field]); - $value = $result[0]['aggregate'] ?? 0; + $value = $result[0]['aggregate'] ?? 0; if ($force) { $value += 0; @@ -423,7 +423,7 @@ public function limit(int $offset, int $length = null) $offset = 0; } - $this->options['skip'] = $offset; + $this->options['skip'] = $offset; $this->options['limit'] = $length; return $this; @@ -514,22 +514,22 @@ public function getQueryGuid($data = null): string public function paginate($listRows = null, $simple = false): Paginator { if (is_int($simple)) { - $total = $simple; + $total = $simple; $simple = false; } $defaultConfig = [ - 'query' => [], //url额外参数 - 'fragment' => '', //url锚点 - 'var_page' => 'page', //分页变量 + 'query' => [], //url额外参数 + 'fragment' => '', //url锚点 + 'var_page' => 'page', //分页变量 'list_rows' => 15, //每页数量 ]; if (is_array($listRows)) { - $config = array_merge($defaultConfig, $listRows); + $config = array_merge($defaultConfig, $listRows); $listRows = intval($config['list_rows']); } else { - $config = $defaultConfig; + $config = $defaultConfig; $listRows = intval($listRows ?: $config['list_rows']); } @@ -544,11 +544,11 @@ public function paginate($listRows = null, $simple = false): Paginator unset($this->options['order'], $this->options['limit'], $this->options['page'], $this->options['field']); - $total = $this->count(); + $total = $this->count(); $results = $this->options($options)->page($page, $listRows)->select(); } elseif ($simple) { $results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select(); - $total = null; + $total = null; } else { $results = $this->page($page, $listRows)->select(); } @@ -572,7 +572,7 @@ public function paginate($listRows = null, $simple = false): Paginator public function chunk(int $count, callable $callback, $column = null, string $order = 'asc'): bool { $options = $this->getOptions(); - $column = $column ?: $this->getPk(); + $column = $column ?: $this->getPk(); if (isset($options['order'])) { unset($options['order']); @@ -602,7 +602,7 @@ public function chunk(int $count, callable $callback, $column = null, string $or $times++; $query = $this->options($options)->page($times, $count); } else { - $end = $resultSet->pop(); + $end = $resultSet->pop(); $lastId = is_array($end) ? $end[$key] : $end->getData($key); $query = $this->options($options) @@ -667,10 +667,10 @@ public function parseOptions(): array // 根据页数计算limit [$page, $listRows] = $options['page']; - $page = $page > 0 ? $page : 1; - $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); - $offset = $listRows * ($page - 1); - $options['skip'] = intval($offset); + $page = $page > 0 ? $page : 1; + $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); + $offset = $listRows * ($page - 1); + $options['skip'] = intval($offset); $options['limit'] = intval($listRows); } diff --git a/src/db/PDOConnection.php b/src/db/PDOConnection.php index 8f89fe3b..de016f82 100644 --- a/src/db/PDOConnection.php +++ b/src/db/PDOConnection.php @@ -38,45 +38,45 @@ abstract class PDOConnection extends Connection */ protected $config = [ // 数据库类型 - 'type' => '', + 'type' => '', // 服务器地址 - 'hostname' => '', + 'hostname' => '', // 数据库名 - 'database' => '', + 'database' => '', // 用户名 - 'username' => '', + 'username' => '', // 密码 - 'password' => '', + 'password' => '', // 端口 - 'hostport' => '', + 'hostport' => '', // 连接dsn - 'dsn' => '', + 'dsn' => '', // 数据库连接参数 - 'params' => [], + 'params' => [], // 数据库编码默认采用utf8 - 'charset' => 'utf8', + 'charset' => 'utf8', // 数据库表前缀 - 'prefix' => '', + 'prefix' => '', // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) - 'deploy' => 0, + 'deploy' => 0, // 数据库读写是否分离 主从式有效 - 'rw_separate' => false, + 'rw_separate' => false, // 读写分离后 主服务器数量 - 'master_num' => 1, + 'master_num' => 1, // 指定从服务器序号 - 'slave_no' => '', + 'slave_no' => '', // 模型写入后自动读取主服务器 - 'read_master' => false, + 'read_master' => false, // 是否严格检查字段是否存在 - 'fields_strict' => true, + 'fields_strict' => true, // 开启字段缓存 - 'fields_cache' => false, + 'fields_cache' => false, // 监听SQL - 'trigger_sql' => true, + 'trigger_sql' => true, // Builder类 - 'builder' => '', + 'builder' => '', // Query类 - 'query' => '', + 'query' => '', // 是否需要断线重连 'break_reconnect' => false, // 断线标识字符串 @@ -138,11 +138,11 @@ abstract class PDOConnection extends Connection * @var array */ protected $params = [ - PDO::ATTR_CASE => PDO::CASE_NATURAL, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, + PDO::ATTR_CASE => PDO::CASE_NATURAL, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, - PDO::ATTR_EMULATE_PREPARES => false, + PDO::ATTR_EMULATE_PREPARES => false, ]; /** @@ -150,14 +150,14 @@ abstract class PDOConnection extends Connection * @var array */ protected $bindType = [ - 'string' => PDO::PARAM_STR, - 'str' => PDO::PARAM_STR, - 'integer' => PDO::PARAM_INT, - 'int' => PDO::PARAM_INT, - 'boolean' => PDO::PARAM_BOOL, - 'bool' => PDO::PARAM_BOOL, - 'float' => self::PARAM_FLOAT, - 'datetime' => PDO::PARAM_STR, + 'string' => PDO::PARAM_STR, + 'str' => PDO::PARAM_STR, + 'integer' => PDO::PARAM_INT, + 'int' => PDO::PARAM_INT, + 'boolean' => PDO::PARAM_BOOL, + 'bool' => PDO::PARAM_BOOL, + 'float' => self::PARAM_FLOAT, + 'datetime' => PDO::PARAM_STR, 'timestamp' => PDO::PARAM_STR, ]; @@ -353,7 +353,7 @@ public function getSchemaInfo(string $tableName, $force = false) if (!isset($this->info[$schema]) || $force) { // 读取字段缓存 - $cacheKey = $this->getSchemaCacheKey($schema); + $cacheKey = $this->getSchemaCacheKey($schema); if ($this->config['fields_cache'] && !empty($this->cache) && !$force) { $info = $this->cache->get($cacheKey); } @@ -365,7 +365,7 @@ public function getSchemaInfo(string $tableName, $force = false) } } - $pk = $info['_pk'] ?? null; + $pk = $info['_pk'] ?? null; $autoinc = $info['_autoinc'] ?? null; unset($info['_pk'], $info['_autoinc']); @@ -375,10 +375,10 @@ public function getSchemaInfo(string $tableName, $force = false) } $this->info[$schema] = [ - 'fields' => array_keys($info), - 'type' => $info, - 'bind' => $bind, - 'pk' => $pk, + 'fields' => array_keys($info), + 'type' => $info, + 'bind' => $bind, + 'pk' => $pk, 'autoinc' => $autoinc, ]; } @@ -420,7 +420,7 @@ public function getTableInfo($tableName, string $fetch = '') public function getTableFieldsInfo(string $tableName): array { $fields = $this->getFields($tableName); - $info = []; + $info = []; foreach ($fields as $key => $val) { // 记录字段类型 @@ -437,7 +437,7 @@ public function getTableFieldsInfo(string $tableName): array if (isset($pk)) { // 设置主键 - $pk = count($pk) > 1 ? $pk : $pk[0]; + $pk = count($pk) > 1 ? $pk : $pk[0]; $info['_pk'] = $pk; } @@ -687,7 +687,7 @@ protected function pdoQuery(BaseQuery $query, $sql, bool $master = null): array // 检查查询缓存 $cacheItem = $this->parseCache($query, $query->getOptions('cache')); if (!$query->getOptions('force_cache')) { - $key = $cacheItem->getKey(); + $key = $cacheItem->getKey(); $data = $this->cache->get($key); @@ -698,7 +698,7 @@ protected function pdoQuery(BaseQuery $query, $sql, bool $master = null): array } if ($sql instanceof Closure) { - $sql = $sql($query); + $sql = $sql($query); $bind = $query->getBind(); } @@ -710,7 +710,7 @@ protected function pdoQuery(BaseQuery $query, $sql, bool $master = null): array $this->getPDOStatement($sql, $bind, $master, $procedure); - $resultSet = $this->getResult($procedure); + $resultSet = $this->getResult($procedure); $requireCache = $query->getOptions('cache_always') || !empty($resultSet); if (isset($cacheItem) && $requireCache) { @@ -753,7 +753,7 @@ public function getPDOStatement(string $sql, array $bind = [], bool $master = fa $this->initConnect($this->readMaster ?: $master); // 记录SQL语句 $this->queryStr = $sql; - $this->bind = $bind; + $this->bind = $bind; $this->db->updateQueryTimes(); $this->queryStartTime = microtime(true); @@ -827,8 +827,8 @@ protected function pdoExecute(BaseQuery $query, string $sql, bool $origin = fals if ($query->getOptions('cache')) { // 清理缓存数据 $cacheItem = $this->parseCache($query, $query->getOptions('cache')); - $key = $cacheItem->getKey(); - $tag = $cacheItem->getTag(); + $key = $cacheItem->getKey(); + $tag = $cacheItem->getTag(); if (isset($key) && $this->cache->has($key)) { $this->cache->delete($key); @@ -848,9 +848,9 @@ protected function pdoExecute(BaseQuery $query, string $sql, bool $origin = fals */ protected function queryPDOStatement(BaseQuery $query, string $sql): PDOStatement { - $options = $query->getOptions(); - $bind = $query->getBind(); - $master = !empty($options['master']) ? true : false; + $options = $query->getOptions(); + $bind = $query->getBind(); + $master = !empty($options['master']) ? true : false; $procedure = !empty($options['procedure']) ? true : in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']); return $this->getPDOStatement($sql, $bind, $master, $procedure); @@ -940,7 +940,7 @@ public function insert(BaseQuery $query, bool $getLastInsID = false) $result = '' == $sql ? 0 : $this->pdoExecute($query, $sql); if ($result) { - $sequence = $options['sequence'] ?? null; + $sequence = $options['sequence'] ?? null; $lastInsId = $this->getLastInsID($query, $sequence); $data = $options['data']; @@ -1156,7 +1156,7 @@ public function value(BaseQuery $query, string $field, $default = null, bool $on $cacheItem = $this->parseCache($query, $options['cache'], 'value'); if (!$query->getOptions('force_cache')) { - $key = $cacheItem->getKey(); + $key = $cacheItem->getKey(); if ($this->cache->has($key)) { return $this->cache->get($key); @@ -1257,7 +1257,7 @@ public function column(BaseQuery $query, $column, string $key = ''): array // 判断查询缓存 $cacheItem = $this->parseCache($query, $options['cache'], 'column'); if (!$query->getOptions('force_cache')) { - $name = $cacheItem->getKey(); + $name = $cacheItem->getKey(); if ($this->cache->has($name)) { return $this->cache->get($name); @@ -1275,7 +1275,7 @@ public function column(BaseQuery $query, $column, string $key = ''): array } // 执行查询操作 - $pdo = $this->getPDOStatement($sql, $query->getBind(), $options['master']); + $pdo = $this->getPDOStatement($sql, $query->getBind(), $options['master']); $resultSet = $pdo->fetchAll(PDO::FETCH_ASSOC); if (is_string($key) && strpos($key, '.')) { @@ -1325,7 +1325,7 @@ public function getRealSql(string $sql, array $bind = []): string { foreach ($bind as $key => $val) { $value = strval(is_array($val) ? $val[0] : $val); - $type = is_array($val) ? $val[1] : PDO::PARAM_STR; + $type = is_array($val) ? $val[1] : PDO::PARAM_STR; if (self::PARAM_FLOAT == $type || PDO::PARAM_STR == $type) { $value = '\'' . addslashes($value) . '\''; @@ -1623,10 +1623,10 @@ public function batchQuery(BaseQuery $query, array $sqlArray = []): bool */ public function close() { - $this->linkID = null; - $this->linkWrite = null; - $this->linkRead = null; - $this->links = []; + $this->linkID = null; + $this->linkWrite = null; + $this->linkRead = null; + $this->links = []; $this->transTimes = 0; $this->free(); diff --git a/src/db/Query.php b/src/db/Query.php index 35e66bcb..6af0701a 100644 --- a/src/db/Query.php +++ b/src/db/Query.php @@ -402,7 +402,7 @@ public function cursor($data = null) public function chunk(int $count, callable $callback, $column = null, string $order = 'asc'): bool { $options = $this->getOptions(); - $column = $column ?: $this->getPk(); + $column = $column ?: $this->getPk(); if (isset($options['order'])) { unset($options['order']); @@ -434,7 +434,7 @@ public function chunk(int $count, callable $callback, $column = null, string $or $times++; $query = $this->options($options)->page($times, $count); } else { - $end = $resultSet->pop(); + $end = $resultSet->pop(); $lastId = is_array($end) ? $end[$key] : $end->getData($key); $query = $this->options($options) diff --git a/src/db/Raw.php b/src/db/Raw.php index b956ff69..f6e5f059 100644 --- a/src/db/Raw.php +++ b/src/db/Raw.php @@ -41,7 +41,7 @@ class Raw public function __construct(string $value, array $bind = []) { $this->value = $value; - $this->bind = $bind; + $this->bind = $bind; } /** diff --git a/src/db/Where.php b/src/db/Where.php index 08804608..da896bdd 100644 --- a/src/db/Where.php +++ b/src/db/Where.php @@ -39,7 +39,7 @@ class Where implements ArrayAccess */ public function __construct(array $where = [], bool $enclose = false) { - $this->where = $where; + $this->where = $where; $this->enclose = $enclose; } @@ -88,7 +88,7 @@ public function parse(): array */ protected function parseItem(string $field, array $where = []): array { - $op = $where[0]; + $op = $where[0]; $condition = $where[1] ?? null; if (is_array($op)) { diff --git a/src/db/builder/Mongo.php b/src/db/builder/Mongo.php index 85ee5519..fc2ec422 100644 --- a/src/db/builder/Mongo.php +++ b/src/db/builder/Mongo.php @@ -193,7 +193,7 @@ public function parseWhere(Query $query, array $where): array } } else { // 对字段使用表达式查询 - $field = is_string($field) ? $field : ''; + $field = is_string($field) ? $field : ''; $filter[$logic][] = $this->parseWhereItem($query, $field, $value); } } @@ -204,7 +204,7 @@ public function parseWhere(Query $query, array $where): array if (!empty($options['soft_delete'])) { // 附加软删除条件 [$field, $condition] = $options['soft_delete']; - $filter['$and'][] = $this->parseWhereItem($query, $field, $condition); + $filter['$and'][] = $this->parseWhereItem($query, $field, $condition); } return $filter; @@ -224,7 +224,7 @@ protected function parseWhereItem(Query $query, $field, $val): array if (is_array($exp)) { $data = []; foreach ($val as $value) { - $exp = $value[0]; + $exp = $value[0]; $value = $value[1]; if (!in_array($exp, $this->exp)) { $exp = strtolower($exp); @@ -232,7 +232,7 @@ protected function parseWhereItem(Query $query, $field, $val): array $exp = $this->exp[$exp]; } } - $k = '$' . $exp; + $k = '$' . $exp; $data[$k] = $value; } $result[$key] = $data; @@ -252,7 +252,7 @@ protected function parseWhereItem(Query $query, $field, $val): array $result[$key] = $this->parseValue($query, $value, $key); } elseif (in_array($exp, ['neq', 'ne', 'gt', 'egt', 'gte', 'lt', 'lte', 'elt', 'mod'])) { // 比较运算 - $k = '$' . $exp; + $k = '$' . $exp; $result[$key] = [$k => $this->parseValue($query, $value, $key)]; } elseif ('null' == $exp) { // NULL 查询 @@ -264,11 +264,11 @@ protected function parseWhereItem(Query $query, $field, $val): array $result[$key] = ['$all', $this->parseValue($query, $value, $key)]; } elseif ('between' == $exp) { // 区间查询 - $value = is_array($value) ? $value : explode(',', $value); + $value = is_array($value) ? $value : explode(',', $value); $result[$key] = ['$gte' => $this->parseValue($query, $value[0], $key), '$lte' => $this->parseValue($query, $value[1], $key)]; } elseif ('not between' == $exp) { // 范围查询 - $value = is_array($value) ? $value : explode(',', $value); + $value = is_array($value) ? $value : explode(',', $value); $result[$key] = ['$lt' => $this->parseValue($query, $value[0], $key), '$gt' => $this->parseValue($query, $value[1], $key)]; } elseif ('exists' == $exp) { // 字段是否存在 @@ -297,11 +297,11 @@ protected function parseWhereItem(Query $query, $field, $val): array $result[$key] = ['$gt' => $this->parseDateTime($query, $value, $field)]; } elseif ('between time' == $exp) { // 区间查询 - $value = is_array($value) ? $value : explode(',', $value); + $value = is_array($value) ? $value : explode(',', $value); $result[$key] = ['$gte' => $this->parseDateTime($query, $value[0], $field), '$lte' => $this->parseDateTime($query, $value[1], $field)]; } elseif ('not between time' == $exp) { // 范围查询 - $value = is_array($value) ? $value : explode(',', $value); + $value = is_array($value) ? $value : explode(',', $value); $result[$key] = ['$lt' => $this->parseDateTime($query, $value[0], $field), '$gt' => $this->parseDateTime($query, $value[1], $field)]; } elseif ('near' == $exp) { // 经纬度查询 @@ -392,7 +392,7 @@ public function insert(Query $query): BulkWrite */ public function insertAll(Query $query, array $dataSet): BulkWrite { - $bulk = new BulkWrite; + $bulk = new BulkWrite; $options = $query->getOptions(); $this->insertId = []; @@ -419,7 +419,7 @@ public function update(Query $query): BulkWrite { $options = $query->getOptions(); - $data = $this->parseSet($query, $options['data']); + $data = $this->parseSet($query, $options['data']); $where = $this->parseWhere($query, $options['where']); if (1 == $options['limit']) { @@ -446,7 +446,7 @@ public function update(Query $query): BulkWrite public function delete(Query $query): BulkWrite { $options = $query->getOptions(); - $where = $this->parseWhere($query, $options['where']); + $where = $this->parseWhere($query, $options['where']); $bulk = new BulkWrite; @@ -521,7 +521,7 @@ public function count(Query $query): Command */ public function aggregate(Query $query, array $extra): Command { - $options = $query->getOptions(); + $options = $query->getOptions(); [$fun, $field] = $extra; if ('id' == $field && $this->connection->getConfig('pk_convert_id')) { @@ -536,10 +536,10 @@ public function aggregate(Query $query, array $extra): Command ]; $cmd = [ - 'aggregate' => $options['table'], + 'aggregate' => $options['table'], 'allowDiskUse' => true, - 'pipeline' => $pipeline, - 'cursor' => new \stdClass, + 'pipeline' => $pipeline, + 'cursor' => new \stdClass, ]; foreach (['explain', 'collation', 'bypassDocumentValidation', 'readConcern'] as $option) { @@ -584,10 +584,10 @@ public function multiAggregate(Query $query, $extra): Command ]; $cmd = [ - 'aggregate' => $options['table'], + 'aggregate' => $options['table'], 'allowDiskUse' => true, - 'pipeline' => $pipeline, - 'cursor' => new \stdClass, + 'pipeline' => $pipeline, + 'cursor' => new \stdClass, ]; foreach (['explain', 'collation', 'bypassDocumentValidation', 'readConcern'] as $option) { @@ -615,7 +615,7 @@ public function distinct(Query $query, $field): Command $cmd = [ 'distinct' => $options['table'], - 'key' => $field, + 'key' => $field, ]; if (!empty($options['where'])) { @@ -640,7 +640,7 @@ public function distinct(Query $query, $field): Command */ public function listcollections(): Command { - $cmd = ['listCollections' => 1]; + $cmd = ['listCollections' => 1]; $command = new Command($cmd); $this->log('cmd', 'listCollections', $cmd); @@ -658,7 +658,7 @@ public function collStats(Query $query): Command { $options = $query->getOptions(); - $cmd = ['collStats' => $options['table']]; + $cmd = ['collStats' => $options['table']]; $command = new Command($cmd); $this->log('cmd', 'collStats', $cmd); diff --git a/src/db/builder/Mysql.php b/src/db/builder/Mysql.php index 7dda33a9..671f0110 100644 --- a/src/db/builder/Mysql.php +++ b/src/db/builder/Mysql.php @@ -28,18 +28,18 @@ class Mysql extends Builder * @var array */ protected $parser = [ - 'parseCompare' => ['=', '<>', '>', '>=', '<', '<='], - 'parseLike' => ['LIKE', 'NOT LIKE'], - 'parseBetween' => ['NOT BETWEEN', 'BETWEEN'], - 'parseIn' => ['NOT IN', 'IN'], - 'parseExp' => ['EXP'], - 'parseRegexp' => ['REGEXP', 'NOT REGEXP'], - 'parseNull' => ['NOT NULL', 'NULL'], + 'parseCompare' => ['=', '<>', '>', '>=', '<', '<='], + 'parseLike' => ['LIKE', 'NOT LIKE'], + 'parseBetween' => ['NOT BETWEEN', 'BETWEEN'], + 'parseIn' => ['NOT IN', 'IN'], + 'parseExp' => ['EXP'], + 'parseRegexp' => ['REGEXP', 'NOT REGEXP'], + 'parseNull' => ['NOT NULL', 'NULL'], 'parseBetweenTime' => ['BETWEEN TIME', 'NOT BETWEEN TIME'], - 'parseTime' => ['< TIME', '> TIME', '<= TIME', '>= TIME'], - 'parseExists' => ['NOT EXISTS', 'EXISTS'], - 'parseColumn' => ['COLUMN'], - 'parseFindInSet' => ['FIND IN SET'], + 'parseTime' => ['< TIME', '> TIME', '<= TIME', '>= TIME'], + 'parseExists' => ['NOT EXISTS', 'EXISTS'], + 'parseColumn' => ['COLUMN'], + 'parseFindInSet' => ['FIND IN SET'], ]; /** @@ -209,7 +209,7 @@ public function insertAllByKeys(Query $query, array $keys, array $datas): string $options = $query->getOptions(); // 获取绑定信息 - $bind = $query->getFieldsBindType(); + $bind = $query->getFieldsBindType(); $fields = []; $values = []; @@ -465,12 +465,12 @@ protected function parseDuplicate(Query $query, $duplicate): string $updates = []; foreach ($duplicate as $key => $val) { if (is_numeric($key)) { - $val = $this->parseKey($query, $val); + $val = $this->parseKey($query, $val); $updates[] = $val . ' = VALUES(' . $val . ')'; } elseif ($val instanceof Raw) { $updates[] = $this->parseKey($query, $key) . " = " . $this->parseRaw($query, $val); } else { - $name = $query->bindValue($val, $query->getConnection()->getFieldBindType($key)); + $name = $query->bindValue($val, $query->getConnection()->getFieldBindType($key)); $updates[] = $this->parseKey($query, $key) . " = :" . $name; } } diff --git a/src/db/builder/Oracle.php b/src/db/builder/Oracle.php index 38feb5dc..c1a78026 100644 --- a/src/db/builder/Oracle.php +++ b/src/db/builder/Oracle.php @@ -85,7 +85,7 @@ public function parseKey(Query $query, $key, bool $strict = false): string if (strpos($key, '->') && false === strpos($key, '(')) { // JSON字段支持 [$field, $name] = explode($key, '->'); - $key = $field . '."' . $name . '"'; + $key = $field . '."' . $name . '"'; } elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)\[\s]/', $key)) { [$table, $key] = explode('.', $key, 2); diff --git a/src/db/builder/Pgsql.php b/src/db/builder/Pgsql.php index 4eace0ac..cf846dbc 100644 --- a/src/db/builder/Pgsql.php +++ b/src/db/builder/Pgsql.php @@ -77,7 +77,7 @@ public function parseKey(Query $query, $key, bool $strict = false): string if (strpos($key, '->') && false === strpos($key, '(')) { // JSON字段支持 [$field, $name] = explode('->', $key); - $key = '"' . $field . '"' . '->>\'' . $name . '\''; + $key = '"' . $field . '"' . '->>\'' . $name . '\''; } elseif (strpos($key, '.')) { [$table, $key] = explode('.', $key, 2); diff --git a/src/db/builder/Sqlsrv.php b/src/db/builder/Sqlsrv.php index 779b5e35..17120ceb 100644 --- a/src/db/builder/Sqlsrv.php +++ b/src/db/builder/Sqlsrv.php @@ -83,7 +83,7 @@ protected function parseOrder(Query $query, array $order): string $sort = $val; } - $sort = in_array(strtolower($sort), ['asc', 'desc'], true) ? ' ' . $sort : ''; + $sort = in_array(strtolower($sort), ['asc', 'desc'], true) ? ' ' . $sort : ''; $array[] = $this->parseKey($query, $key, true) . $sort; } } diff --git a/src/db/concern/JoinAndViewQuery.php b/src/db/concern/JoinAndViewQuery.php index 28289497..59bd8646 100644 --- a/src/db/concern/JoinAndViewQuery.php +++ b/src/db/concern/JoinAndViewQuery.php @@ -143,7 +143,7 @@ public function view($join, $field = true, $on = null, string $type = 'INNER', a $this->options['view'] = true; $fields = []; - $table = $this->getJoinTable($join, $alias); + $table = $this->getJoinTable($join, $alias); if (true === $field) { $fields = $alias . '.*'; diff --git a/src/db/concern/ModelRelationQuery.php b/src/db/concern/ModelRelationQuery.php index cabd65bb..9fff5d86 100644 --- a/src/db/concern/ModelRelationQuery.php +++ b/src/db/concern/ModelRelationQuery.php @@ -163,7 +163,7 @@ public function withSearch($fields, $data = [], string $prefix = '') } elseif ($this->model) { // 检测搜索器 $fieldName = is_numeric($key) ? $field : $key; - $method = 'search' . Str::studly($fieldName) . 'Attr'; + $method = 'search' . Str::studly($fieldName) . 'Attr'; if (method_exists($this->model, $method)) { $this->model->$method($this, $data[$field] ?? null, $data, $prefix); @@ -237,19 +237,19 @@ public function withJoin($with, string $joinType = '') return $this; } - $with = (array) $with; + $with = (array) $with; $first = true; foreach ($with as $key => $relation) { $closure = null; - $field = true; + $field = true; if ($relation instanceof Closure) { // 支持闭包查询过滤关联条件 - $closure = $relation; + $closure = $relation; $relation = $key; } elseif (is_array($relation)) { - $field = $relation; + $field = $relation; $relation = $key; } elseif (is_string($relation) && strpos($relation, '.')) { $relation = strstr($relation, '.', true); @@ -319,7 +319,7 @@ public function withCache($relation = true, $key = true, $expire = null, string if ($key instanceof \DateTimeInterface || $key instanceof \DateInterval || (is_int($key) && is_null($expire))) { $expire = $key; - $key = true; + $key = true; } if (true === $relation || is_numeric($relation)) { diff --git a/src/db/concern/TimeFieldQuery.php b/src/db/concern/TimeFieldQuery.php index 69b7eae4..460f826c 100644 --- a/src/db/concern/TimeFieldQuery.php +++ b/src/db/concern/TimeFieldQuery.php @@ -22,14 +22,14 @@ trait TimeFieldQuery * @var array */ protected $timeRule = [ - 'today' => ['today', 'tomorrow -1second'], - 'yesterday' => ['yesterday', 'today -1second'], - 'week' => ['this week 00:00:00', 'next week 00:00:00 -1second'], - 'last week' => ['last week 00:00:00', 'this week 00:00:00 -1second'], - 'month' => ['first Day of this month 00:00:00', 'first Day of next month 00:00:00 -1second'], + 'today' => ['today', 'tomorrow -1second'], + 'yesterday' => ['yesterday', 'today -1second'], + 'week' => ['this week 00:00:00', 'next week 00:00:00 -1second'], + 'last week' => ['last week 00:00:00', 'this week 00:00:00 -1second'], + 'month' => ['first Day of this month 00:00:00', 'first Day of next month 00:00:00 -1second'], 'last month' => ['first Day of last month 00:00:00', 'first Day of this month 00:00:00 -1second'], - 'year' => ['this year 1/1', 'next year 1/1 -1second'], - 'last year' => ['last year 1/1', 'this year 1/1 -1second'], + 'year' => ['this year 1/1', 'next year 1/1 -1second'], + 'last year' => ['last year 1/1', 'this year 1/1 -1second'], ]; /** @@ -80,7 +80,7 @@ public function whereTime(string $field, string $op, $range = null, string $logi public function whereTimeInterval(string $field, string $start, string $interval = 'day', int $step = 1, string $logic = 'AND') { $startTime = strtotime($start); - $endTime = strtotime(($step > 0 ? '+' : '-') . abs($step) . ' ' . $interval . (abs($step) > 1 ? 's' : ''), $startTime); + $endTime = strtotime(($step > 0 ? '+' : '-') . abs($step) . ' ' . $interval . (abs($step) > 1 ? 's' : ''), $startTime); return $this->whereTime($field, 'between', $step > 0 ? [$startTime, $endTime - 1] : [$endTime, $startTime - 1], $logic); } diff --git a/src/db/concern/WhereQuery.php b/src/db/concern/WhereQuery.php index f1b49dbc..a39518c6 100644 --- a/src/db/concern/WhereQuery.php +++ b/src/db/concern/WhereQuery.php @@ -256,7 +256,7 @@ public function whereFindInSet(string $field, $condition, string $logic = 'AND') public function whereColumn(string $field1, string $operator, string $field2 = null, string $logic = 'AND') { if (is_null($field2)) { - $field2 = $operator; + $field2 = $operator; $operator = '='; } @@ -308,7 +308,7 @@ public function whereFieldRaw(string $field, $op, $condition = null, string $log { if (is_null($condition)) { $condition = $op; - $op = '='; + $op = '='; } $this->options['where'][$logic][] = [new Raw($field), $op, $condition]; diff --git a/src/db/connector/Mongo.php b/src/db/connector/Mongo.php index 306e4767..35f8e89e 100644 --- a/src/db/connector/Mongo.php +++ b/src/db/connector/Mongo.php @@ -41,7 +41,7 @@ class Mongo extends Connection { // 查询数据类型 - protected $dbName = ''; + protected $dbName = ''; protected $typeMap = 'array'; protected $mongo; // MongoDb Object protected $cursor; // MongoCursor Object @@ -54,53 +54,53 @@ class Mongo extends Connection // 数据库连接参数配置 protected $config = [ // 数据库类型 - 'type' => '', + 'type' => '', // 服务器地址 - 'hostname' => '', + 'hostname' => '', // 数据库名 - 'database' => '', + 'database' => '', // 是否是复制集 - 'is_replica_set' => false, + 'is_replica_set' => false, // 用户名 - 'username' => '', + 'username' => '', // 密码 - 'password' => '', + 'password' => '', // 端口 - 'hostport' => '', + 'hostport' => '', // 连接dsn - 'dsn' => '', + 'dsn' => '', // 数据库连接参数 - 'params' => [], + 'params' => [], // 数据库编码默认采用utf8 - 'charset' => 'utf8', + 'charset' => 'utf8', // 主键名 - 'pk' => '_id', + 'pk' => '_id', // 主键类型 - 'pk_type' => 'ObjectID', + 'pk_type' => 'ObjectID', // 数据库表前缀 - 'prefix' => '', + 'prefix' => '', // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) - 'deploy' => 0, + 'deploy' => 0, // 数据库读写是否分离 主从式有效 - 'rw_separate' => false, + 'rw_separate' => false, // 读写分离后 主服务器数量 - 'master_num' => 1, + 'master_num' => 1, // 指定从服务器序号 - 'slave_no' => '', + 'slave_no' => '', // 是否严格检查字段是否存在 - 'fields_strict' => true, + 'fields_strict' => true, // 开启字段缓存 - 'fields_cache' => false, + 'fields_cache' => false, // 监听SQL - 'trigger_sql' => true, + 'trigger_sql' => true, // 自动写入时间戳字段 - 'auto_timestamp' => false, + 'auto_timestamp' => false, // 时间字段取出后的默认时间格式 'datetime_format' => 'Y-m-d H:i:s', // 是否_id转换为id - 'pk_convert_id' => false, + 'pk_convert_id' => false, // typeMap - 'type_map' => ['root' => 'array', 'document' => 'array'], + 'type_map' => ['root' => 'array', 'document' => 'array'], ]; /** @@ -151,7 +151,7 @@ public function connect(array $config = [], $linkNum = 0) $config = array_merge($this->config, $config); } - $this->dbName = $config['database']; + $this->dbName = $config['database']; $this->typeMap = $config['type_map']; if ($config['pk_convert_id'] && '_id' == $config['pk']) { @@ -238,7 +238,7 @@ public function getCursor(BaseQuery $query, $mongoQuery, bool $master = false): $this->initConnect($master); $this->db->updateQueryTimes(); - $options = $query->getOptions(); + $options = $query->getOptions(); $namespace = $options['table']; if (false === strpos($namespace, '.')) { @@ -254,13 +254,13 @@ public function getCursor(BaseQuery $query, $mongoQuery, bool $master = false): $mongoQuery = $mongoQuery($query); } - $readPreference = $options['readPreference'] ?? null; + $readPreference = $options['readPreference'] ?? null; $this->queryStartTime = microtime(true); if ($session = $this->getSession()) { $this->cursor = $this->mongo->executeQuery($namespace, $mongoQuery, [ 'readPreference' => is_null($readPreference) ? new ReadPreference(ReadPreference::RP_PRIMARY) : $readPreference, - 'session' => $session, + 'session' => $session, ]); } else { $this->cursor = $this->mongo->executeQuery($namespace, $mongoQuery, $readPreference); @@ -323,7 +323,7 @@ protected function mongoQuery(BaseQuery $query, $mongoQuery): array if ($query->getOptions('cache')) { // 检查查询缓存 $cacheItem = $this->parseCache($query, $query->getOptions('cache')); - $key = $cacheItem->getKey(); + $key = $cacheItem->getKey(); if ($this->cache->has($key)) { return $this->cache->get($key); @@ -378,12 +378,12 @@ protected function mongoExecute(BaseQuery $query, BulkWrite $bulk) $this->queryStr = 'db' . strstr($namespace, '.') . '.' . $this->queryStr; } - $writeConcern = $options['writeConcern'] ?? null; + $writeConcern = $options['writeConcern'] ?? null; $this->queryStartTime = microtime(true); if ($session = $this->getSession()) { $writeResult = $this->mongo->executeBulkWrite($namespace, $bulk, [ - 'session' => $session, + 'session' => $session, 'writeConcern' => is_null($writeConcern) ? new WriteConcern(1) : $writeConcern, ]); } else { @@ -400,8 +400,8 @@ protected function mongoExecute(BaseQuery $query, BulkWrite $bulk) if ($query->getOptions('cache')) { // 清理缓存数据 $cacheItem = $this->parseCache($query, $query->getOptions('cache')); - $key = $cacheItem->getKey(); - $tag = $cacheItem->getTag(); + $key = $cacheItem->getKey(); + $tag = $cacheItem->getTag(); if (isset($key) && $this->cache->has($key)) { $this->cache->delete($key); @@ -443,7 +443,7 @@ public function command(Command $command, string $dbName = '', ReadPreference $r if ($session = $this->getSession()) { $this->cursor = $this->mongo->executeCommand($dbName, $command, [ 'readPreference' => is_null($readPreference) ? new ReadPreference(ReadPreference::RP_PRIMARY) : $readPreference, - 'session' => $session, + 'session' => $session, ]); } else { $this->cursor = $this->mongo->executeCommand($dbName, $command, $readPreference); @@ -577,11 +577,11 @@ public function getLastSql(): string */ public function close() { - $this->mongo = null; - $this->cursor = null; - $this->linkRead = null; + $this->mongo = null; + $this->cursor = null; + $this->linkRead = null; $this->linkWrite = null; - $this->links = []; + $this->links = []; } /** @@ -666,7 +666,7 @@ protected function multiConnect(bool $master = false): Manager */ public function replicaSetConnect(): Manager { - $this->dbName = $this->config['database']; + $this->dbName = $this->config['database']; $this->typeMap = $this->config['type_map']; $startTime = microtime(true); @@ -726,14 +726,14 @@ public function insert(BaseQuery $query, bool $getLastInsID = false) $bulk = $this->builder->insert($query); $writeResult = $this->mongoExecute($query, $bulk); - $result = $writeResult->getInsertedCount(); + $result = $writeResult->getInsertedCount(); if ($result) { - $data = $options['data']; + $data = $options['data']; $lastInsId = $this->getLastInsID($query); if ($lastInsId) { - $pk = $query->getPk(); + $pk = $query->getPk(); $data[$pk] = $lastInsId; } @@ -936,7 +936,7 @@ public function value(BaseQuery $query, string $field, $default = null) if (!empty($options['cache'])) { $cacheItem = $this->parseCache($query, $options['cache']); - $key = $cacheItem->getKey(); + $key = $cacheItem->getKey(); if ($this->cache->has($key)) { return $this->cache->get($key); @@ -955,7 +955,7 @@ public function value(BaseQuery $query, string $field, $default = null) $resultSet = $this->mongoQuery($query, $mongoQuery); if (!empty($resultSet)) { - $data = array_shift($resultSet); + $data = array_shift($resultSet); $result = $data[$field]; } else { $result = false; @@ -1000,7 +1000,7 @@ public function column(BaseQuery $query, $field, string $key = ''): array if (!empty($options['cache'])) { // 判断查询缓存 $cacheItem = $this->parseCache($query, $options['cache']); - $key = $cacheItem->getKey(); + $key = $cacheItem->getKey(); if ($this->cache->has($key)) { return $this->cache->get($key); @@ -1109,7 +1109,7 @@ public function transaction(callable $callback) public function startTrans() { $this->initConnect(true); - $this->session_uuid = uniqid(); + $this->session_uuid = uniqid(); $this->sessions[$this->session_uuid] = $this->getMongo()->startSession(); $this->sessions[$this->session_uuid]->startTransaction([]); diff --git a/src/db/connector/Mysql.php b/src/db/connector/Mysql.php index fd9e63a4..1881bc51 100644 --- a/src/db/connector/Mysql.php +++ b/src/db/connector/Mysql.php @@ -62,18 +62,18 @@ public function getFields(string $tableName): array $tableName = '`' . $tableName . '`'; } - $sql = 'SHOW FULL COLUMNS FROM ' . $tableName; - $pdo = $this->getPDOStatement($sql); + $sql = 'SHOW FULL COLUMNS FROM ' . $tableName; + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; if (!empty($result)) { foreach ($result as $key => $val) { $val = array_change_key_case($val); $info[$val['field']] = [ - 'name' => $val['field'], - 'type' => $val['type'], + 'name' => $val['field'], + 'type' => $val['type'], 'notnull' => 'NO' == $val['null'], 'default' => $val['default'], 'primary' => strtolower($val['key']) == 'pri', @@ -94,10 +94,10 @@ public function getFields(string $tableName): array */ public function getTables(string $dbName = ''): array { - $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; - $pdo = $this->getPDOStatement($sql); + $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); diff --git a/src/db/connector/Oracle.php b/src/db/connector/Oracle.php index c8e957a0..367aaa53 100644 --- a/src/db/connector/Oracle.php +++ b/src/db/connector/Oracle.php @@ -51,19 +51,19 @@ protected function parseDsn(array $config): string public function getFields(string $tableName): array { [$tableName] = explode(' ', $tableName); - $sql = "select a.column_name,data_type,DECODE (nullable, 'Y', 0, 1) notnull,data_default, DECODE (A .column_name,b.column_name,1,0) pk from all_tab_columns a,(select column_name from all_constraints c, all_cons_columns col where c.constraint_name = col.constraint_name and c.constraint_type = 'P' and c.table_name = '" . $tableName . "' ) b where table_name = '" . $tableName . "' and a.column_name = b.column_name (+)"; + $sql = "select a.column_name,data_type,DECODE (nullable, 'Y', 0, 1) notnull,data_default, DECODE (A .column_name,b.column_name,1,0) pk from all_tab_columns a,(select column_name from all_constraints c, all_cons_columns col where c.constraint_name = col.constraint_name and c.constraint_type = 'P' and c.table_name = '" . $tableName . "' ) b where table_name = '" . $tableName . "' and a.column_name = b.column_name (+)"; - $pdo = $this->getPDOStatement($sql); + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; if ($result) { foreach ($result as $key => $val) { $val = array_change_key_case($val); $info[$val['column_name']] = [ - 'name' => $val['column_name'], - 'type' => $val['data_type'], + 'name' => $val['column_name'], + 'type' => $val['data_type'], 'notnull' => $val['notnull'], 'default' => $val['data_default'], 'primary' => $val['pk'], @@ -83,10 +83,10 @@ public function getFields(string $tableName): array */ public function getTables(string $dbName = ''): array { - $sql = 'select table_name from all_tables'; - $pdo = $this->getPDOStatement($sql); + $sql = 'select table_name from all_tables'; + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); @@ -105,7 +105,7 @@ public function getTables(string $dbName = ''): array public function getLastInsID(BaseQuery $query, string $sequence = null) { if(!is_null($sequence)) { - $pdo = $this->linkID->query("select {$sequence}.currval as id from dual"); + $pdo = $this->linkID->query("select {$sequence}.currval as id from dual"); $result = $pdo->fetchColumn(); } diff --git a/src/db/connector/Pgsql.php b/src/db/connector/Pgsql.php index fec8f840..89d661d0 100644 --- a/src/db/connector/Pgsql.php +++ b/src/db/connector/Pgsql.php @@ -25,9 +25,9 @@ class Pgsql extends PDOConnection * @var array */ protected $params = [ - PDO::ATTR_CASE => PDO::CASE_NATURAL, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, + PDO::ATTR_CASE => PDO::CASE_NATURAL, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, ]; @@ -57,19 +57,19 @@ protected function parseDsn(array $config): string public function getFields(string $tableName): array { [$tableName] = explode(' ', $tableName); - $sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $tableName . '\');'; + $sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $tableName . '\');'; - $pdo = $this->getPDOStatement($sql); + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; if (!empty($result)) { foreach ($result as $key => $val) { $val = array_change_key_case($val); $info[$val['field']] = [ - 'name' => $val['field'], - 'type' => $val['type'], + 'name' => $val['field'], + 'type' => $val['type'], 'notnull' => (bool) ('' !== $val['null']), 'default' => $val['default'], 'primary' => !empty($val['key']), @@ -89,10 +89,10 @@ public function getFields(string $tableName): array */ public function getTables(string $dbName = ''): array { - $sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'"; - $pdo = $this->getPDOStatement($sql); + $sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'"; + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); diff --git a/src/db/connector/Sqlite.php b/src/db/connector/Sqlite.php index 3e42a909..79fd2666 100644 --- a/src/db/connector/Sqlite.php +++ b/src/db/connector/Sqlite.php @@ -42,19 +42,19 @@ protected function parseDsn(array $config): string public function getFields(string $tableName): array { [$tableName] = explode(' ', $tableName); - $sql = 'PRAGMA table_info( \'' . $tableName . '\' )'; + $sql = 'PRAGMA table_info( \'' . $tableName . '\' )'; - $pdo = $this->getPDOStatement($sql); + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; if (!empty($result)) { foreach ($result as $key => $val) { $val = array_change_key_case($val); $info[$val['name']] = [ - 'name' => $val['name'], - 'type' => $val['type'], + 'name' => $val['name'], + 'type' => $val['type'], 'notnull' => 1 === $val['notnull'], 'default' => $val['dflt_value'], 'primary' => '1' == $val['pk'], @@ -78,9 +78,9 @@ public function getTables(string $dbName = ''): array . "UNION ALL SELECT name FROM sqlite_temp_master " . "WHERE type='table' ORDER BY name"; - $pdo = $this->getPDOStatement($sql); + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); diff --git a/src/db/connector/Sqlsrv.php b/src/db/connector/Sqlsrv.php index 835605d2..9ba3ffcb 100644 --- a/src/db/connector/Sqlsrv.php +++ b/src/db/connector/Sqlsrv.php @@ -24,9 +24,9 @@ class Sqlsrv extends PDOConnection * @var array */ protected $params = [ - PDO::ATTR_CASE => PDO::CASE_NATURAL, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, + PDO::ATTR_CASE => PDO::CASE_NATURAL, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, ]; @@ -69,17 +69,17 @@ public function getFields(string $tableName): array AND t.table_name = c.table_name WHERE t.table_name = '$tableName'"; - $pdo = $this->getPDOStatement($sql); + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; if (!empty($result)) { foreach ($result as $key => $val) { $val = array_change_key_case($val); $info[$val['column_name']] = [ - 'name' => $val['column_name'], - 'type' => $val['data_type'], + 'name' => $val['column_name'], + 'type' => $val['data_type'], 'notnull' => (bool) ('' === $val['is_nullable']), // not null is empty, null is yes 'default' => $val['column_default'], 'primary' => false, @@ -88,8 +88,8 @@ public function getFields(string $tableName): array } } - $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'"; - $pdo = $this->linkID->query($sql); + $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'"; + $pdo = $this->linkID->query($sql); $result = $pdo->fetch(PDO::FETCH_ASSOC); if ($result) { @@ -112,9 +112,9 @@ public function getTables(string $dbName = ''): array WHERE TABLE_TYPE = 'BASE TABLE' "; - $pdo = $this->getPDOStatement($sql); + $pdo = $this->getPDOStatement($sql); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); diff --git a/src/db/exception/DataNotFoundException.php b/src/db/exception/DataNotFoundException.php index d10dd433..0e447a99 100644 --- a/src/db/exception/DataNotFoundException.php +++ b/src/db/exception/DataNotFoundException.php @@ -26,7 +26,7 @@ class DataNotFoundException extends DbException public function __construct(string $message, string $table = '', array $config = []) { $this->message = $message; - $this->table = $table; + $this->table = $table; $this->setData('Database Config', $config); } diff --git a/src/db/exception/DbException.php b/src/db/exception/DbException.php index f68b21c0..ff591b21 100644 --- a/src/db/exception/DbException.php +++ b/src/db/exception/DbException.php @@ -30,12 +30,12 @@ class DbException extends Exception public function __construct(string $message, array $config = [], string $sql = '', int $code = 10500) { $this->message = $message; - $this->code = $code; + $this->code = $code; $this->setData('Database Status', [ - 'Error Code' => $code, + 'Error Code' => $code, 'Error Message' => $message, - 'Error SQL' => $sql, + 'Error SQL' => $sql, ]); unset($config['username'], $config['password']); diff --git a/src/db/exception/ModelNotFoundException.php b/src/db/exception/ModelNotFoundException.php index 84a15257..92b860ea 100644 --- a/src/db/exception/ModelNotFoundException.php +++ b/src/db/exception/ModelNotFoundException.php @@ -26,7 +26,7 @@ class ModelNotFoundException extends DbException public function __construct(string $message, string $model = '', array $config = []) { $this->message = $message; - $this->model = $model; + $this->model = $model; $this->setData('Database Config', $config); } diff --git a/src/db/exception/PDOException.php b/src/db/exception/PDOException.php index efe78b96..452fa904 100644 --- a/src/db/exception/PDOException.php +++ b/src/db/exception/PDOException.php @@ -28,13 +28,13 @@ class PDOException extends DbException */ public function __construct(\PDOException $exception, array $config = [], string $sql = '', int $code = 10501) { - $error = $exception->errorInfo; + $error = $exception->errorInfo; $message = $exception->getMessage(); if (!empty($error)) { $this->setData('PDO Error Info', [ - 'SQLSTATE' => $error[0], - 'Driver Error Code' => isset($error[1]) ? $error[1] : 0, + 'SQLSTATE' => $error[0], + 'Driver Error Code' => isset($error[1]) ? $error[1] : 0, 'Driver Error Message' => isset($error[2]) ? $error[2] : '', ]); } diff --git a/src/model/Collection.php b/src/model/Collection.php index 079e8565..71682e7e 100644 --- a/src/model/Collection.php +++ b/src/model/Collection.php @@ -229,7 +229,7 @@ public function diff($items, string $indexKey = null) return new static($items); } - $diff = []; + $diff = []; $dictionary = $this->dictionary($items, $indexKey); if (is_string($indexKey)) { @@ -257,7 +257,7 @@ public function intersect($items, string $indexKey = null) return new static([]); } - $intersect = []; + $intersect = []; $dictionary = $this->dictionary($items, $indexKey); if (is_string($indexKey)) { diff --git a/src/model/Pivot.php b/src/model/Pivot.php index ac16d9e0..264406dd 100644 --- a/src/model/Pivot.php +++ b/src/model/Pivot.php @@ -63,7 +63,7 @@ public function newInstance(array $data = [], $where = null, array $options = [] $model = parent::newInstance($data, $where, $options); $model->parent = $this->parent; - $model->name = $this->name; + $model->name = $this->name; return $model; } diff --git a/src/model/Relation.php b/src/model/Relation.php index 7bb6d9ff..d1d6698d 100644 --- a/src/model/Relation.php +++ b/src/model/Relation.php @@ -261,7 +261,7 @@ protected function getDefaultModel() $model = (new $this->model)->data($this->default); } elseif ($this->default instanceof Closure) { $closure = $this->default; - $model = new $this->model; + $model = new $this->model; $closure($model); } else { $model = $this->default; @@ -278,10 +278,10 @@ protected function getDefaultModel() protected function getClosureType(Closure $closure, $query = null) { $reflect = new ReflectionFunction($closure); - $params = $reflect->getParameters(); + $params = $reflect->getParameters(); if (!empty($params)) { - $type = $params[0]->getType(); + $type = $params[0]->getType(); $query = $query ?: $this->query; return is_null($type) || Relation::class == $type->getName() ? $this : $query; } diff --git a/src/model/concern/Attribute.php b/src/model/concern/Attribute.php index 4359a4c3..8572b45f 100644 --- a/src/model/concern/Attribute.php +++ b/src/model/concern/Attribute.php @@ -450,7 +450,7 @@ protected function writeTransform($value, $type) // no break case 'json': $option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE; - $value = json_encode($value, $option); + $value = json_encode($value, $option); break; case 'serialize': $value = serialize($value); @@ -476,10 +476,10 @@ public function getAttr(string $name) { try { $relation = false; - $value = $this->getData($name); + $value = $this->getData($name); } catch (InvalidArgumentException $e) { $relation = $this->isRelationAttr($name); - $value = null; + $value = null; } return $this->getValue($name, $value, $relation); @@ -612,13 +612,13 @@ protected function readTransform($value, $type) case 'timestamp': if (!is_null($value)) { $format = !empty($param) ? $param : $this->dateFormat; - $value = $this->formatDateTime($format, $value, true); + $value = $this->formatDateTime($format, $value, true); } break; case 'datetime': if (!is_null($value)) { $format = !empty($param) ? $param : $this->dateFormat; - $value = $this->formatDateTime($format, $value); + $value = $this->formatDateTime($format, $value); } break; case 'json': diff --git a/src/model/concern/Conversion.php b/src/model/concern/Conversion.php index 136a90fd..1e588dd6 100644 --- a/src/model/concern/Conversion.php +++ b/src/model/concern/Conversion.php @@ -258,7 +258,7 @@ public function toArray(): array if (isset($this->mapping[$key])) { // 检查字段映射 - $mapName = $this->mapping[$key]; + $mapName = $this->mapping[$key]; $item[$mapName] = $item[$key]; unset($item[$key]); } @@ -281,16 +281,16 @@ protected function appendAttrToArray(array &$item, $key, $name, array $visible, { if (is_array($name)) { // 批量追加关联对象属性 - $relation = $this->getRelationWith($key, $hidden, $visible); + $relation = $this->getRelationWith($key, $hidden, $visible); $item[$key] = $relation ? $relation->append($name)->toArray() : []; } elseif (strpos($name, '.')) { // 追加单个关联对象属性 - [$key, $attr] = explode('.', $name); - $relation = $this->getRelationWith($key, $hidden, $visible); - $item[$key] = $relation ? $relation->append([$attr])->toArray() : []; + [$key, $attr] = explode('.', $name); + $relation = $this->getRelationWith($key, $hidden, $visible); + $item[$key] = $relation ? $relation->append([$attr])->toArray() : []; } else { - $value = $this->getAttr($name); - $item[$name] = $value; + $value = $this->getAttr($name); + $item[$name] = $value; $this->getBindAttrValue($name, $value, $item); } @@ -298,7 +298,7 @@ protected function appendAttrToArray(array &$item, $key, $name, array $visible, protected function getRelationWith(string $key, array $hidden, array $visible) { - $relation = $this->getRelation($key, true); + $relation = $this->getRelation($key, true); if ($relation) { if (isset($visible[$key])) { $relation->visible($visible[$key]); diff --git a/src/model/concern/OptimLock.php b/src/model/concern/OptimLock.php index 5e613183..e9082d78 100644 --- a/src/model/concern/OptimLock.php +++ b/src/model/concern/OptimLock.php @@ -65,7 +65,7 @@ protected function updateLockVersion(): void public function getWhere() { - $where = parent::getWhere(); + $where = parent::getWhere(); $optimLock = $this->getOptimLockField(); if ($optimLock && $lockVer = $this->getOrigin($optimLock)) { diff --git a/src/model/concern/RelationShip.php b/src/model/concern/RelationShip.php index 37b8af87..11d0ac76 100644 --- a/src/model/concern/RelationShip.php +++ b/src/model/concern/RelationShip.php @@ -137,22 +137,22 @@ public function relationQuery(array $relations, array $withRelationAttr = []): v { foreach ($relations as $key => $relation) { $subRelation = []; - $closure = null; + $closure = null; if ($relation instanceof Closure) { // 支持闭包查询过滤关联条件 - $closure = $relation; + $closure = $relation; $relation = $key; } if (is_array($relation)) { $subRelation = $relation; - $relation = $key; + $relation = $key; } elseif (strpos($relation, '.')) { [$relation, $subRelation] = explode('.', $relation, 2); } - $method = Str::camel($relation); + $method = Str::camel($relation); $relationName = Str::snake($relation); $relationResult = $this->$method(); @@ -229,7 +229,7 @@ public static function hasWhere(string $relation, $where = [], string $fields = public function eagerly(Query $query, string $relation, $field, string $joinType = '', Closure $closure = null, bool $first = false): bool { $relation = Str::camel($relation); - $class = $this->$relation(); + $class = $this->$relation(); if ($class instanceof OneToOne) { $class->eagerly($query, $relation, $field, $joinType, $closure, $first); @@ -253,16 +253,16 @@ public function eagerlyResultSet(array &$resultSet, array $relations, array $wit { foreach ($relations as $key => $relation) { $subRelation = []; - $closure = null; + $closure = null; if ($relation instanceof Closure) { - $closure = $relation; + $closure = $relation; $relation = $key; } if (is_array($relation)) { $subRelation = $relation; - $relation = $key; + $relation = $key; } elseif (strpos($relation, '.')) { [$relation, $subRelation] = explode('.', $relation, 2); @@ -270,7 +270,7 @@ public function eagerlyResultSet(array &$resultSet, array $relations, array $wit } $relationName = $relation; - $relation = Str::camel($relation); + $relation = Str::camel($relation); $relationResult = $this->$relation(); @@ -301,16 +301,16 @@ public function eagerlyResult(array $relations, array $withRelationAttr = [], bo { foreach ($relations as $key => $relation) { $subRelation = []; - $closure = null; + $closure = null; if ($relation instanceof Closure) { - $closure = $relation; + $closure = $relation; $relation = $key; } if (is_array($relation)) { $subRelation = $relation; - $relation = $key; + $relation = $key; } elseif (strpos($relation, '.')) { [$relation, $subRelation] = explode('.', $relation, 2); @@ -318,7 +318,7 @@ public function eagerlyResult(array $relations, array $withRelationAttr = [], bo } $relationName = $relation; - $relation = Str::camel($relation); + $relation = Str::camel($relation); $relationResult = $this->$relation(); @@ -349,7 +349,7 @@ public function bindAttr(string $relation, array $attrs = []) $relation = $this->getRelation($relation, true); foreach ($attrs as $key => $attr) { - $key = is_numeric($key) ? $attr : $key; + $key = is_numeric($key) ? $attr : $key; $value = $this->getOrigin($key); if (!is_null($value)) { @@ -378,10 +378,10 @@ public function relationCount(Query $query, array $relations, string $aggregate $closure = $name = null; if ($relation instanceof Closure) { - $closure = $relation; + $closure = $relation; $relation = $key; } elseif (is_string($key)) { - $name = $relation; + $name = $relation; $relation = $key; } @@ -416,8 +416,8 @@ public function relationCount(Query $query, array $relations, string $aggregate public function hasOne(string $model, string $foreignKey = '', string $localKey = ''): HasOne { // 记录当前关联信息 - $model = $this->parseModel($model); - $localKey = $localKey ?: $this->getPk(); + $model = $this->parseModel($model); + $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); return new HasOne($this, $model, $foreignKey, $localKey); @@ -434,11 +434,11 @@ public function hasOne(string $model, string $foreignKey = '', string $localKey public function belongsTo(string $model, string $foreignKey = '', string $localKey = ''): BelongsTo { // 记录当前关联信息 - $model = $this->parseModel($model); + $model = $this->parseModel($model); $foreignKey = $foreignKey ?: $this->getForeignKey((new $model)->getName()); - $localKey = $localKey ?: (new $model)->getPk(); - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); - $relation = Str::snake($trace[1]['function']); + $localKey = $localKey ?: (new $model)->getPk(); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); + $relation = Str::snake($trace[1]['function']); return new BelongsTo($this, $model, $foreignKey, $localKey, $relation); } @@ -454,8 +454,8 @@ public function belongsTo(string $model, string $foreignKey = '', string $localK public function hasMany(string $model, string $foreignKey = '', string $localKey = ''): HasMany { // 记录当前关联信息 - $model = $this->parseModel($model); - $localKey = $localKey ?: $this->getPk(); + $model = $this->parseModel($model); + $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); return new HasMany($this, $model, $foreignKey, $localKey); @@ -475,12 +475,12 @@ public function hasMany(string $model, string $foreignKey = '', string $localKey public function hasManyThrough(string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = ''): HasManyThrough { // 记录当前关联信息 - $model = $this->parseModel($model); - $through = $this->parseModel($through); - $localKey = $localKey ?: $this->getPk(); + $model = $this->parseModel($model); + $through = $this->parseModel($through); + $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); $throughKey = $throughKey ?: $this->getForeignKey((new $through)->getName()); - $throughPk = $throughPk ?: (new $through)->getPk(); + $throughPk = $throughPk ?: (new $through)->getPk(); return new HasManyThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $throughPk); } @@ -499,12 +499,12 @@ public function hasManyThrough(string $model, string $through, string $foreignKe public function hasOneThrough(string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = ''): HasOneThrough { // 记录当前关联信息 - $model = $this->parseModel($model); - $through = $this->parseModel($through); - $localKey = $localKey ?: $this->getPk(); + $model = $this->parseModel($model); + $through = $this->parseModel($through); + $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); $throughKey = $throughKey ?: $this->getForeignKey((new $through)->getName()); - $throughPk = $throughPk ?: (new $through)->getPk(); + $throughPk = $throughPk ?: (new $through)->getPk(); return new HasOneThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $throughPk); } @@ -521,11 +521,11 @@ public function hasOneThrough(string $model, string $through, string $foreignKey public function belongsToMany(string $model, string $middle = '', string $foreignKey = '', string $localKey = ''): BelongsToMany { // 记录当前关联信息 - $model = $this->parseModel($model); - $name = Str::snake(class_basename($model)); - $middle = $middle ?: Str::snake($this->name) . '_' . $name; + $model = $this->parseModel($model); + $name = Str::snake(class_basename($model)); + $middle = $middle ?: Str::snake($this->name) . '_' . $name; $foreignKey = $foreignKey ?: $name . '_id'; - $localKey = $localKey ?: $this->getForeignKey($this->name); + $localKey = $localKey ?: $this->getForeignKey($this->name); return new BelongsToMany($this, $model, $middle, $foreignKey, $localKey); } @@ -551,7 +551,7 @@ public function morphOne(string $model, $morph = null, string $type = ''): Morph if (is_array($morph)) { [$morphType, $foreignKey] = $morph; } else { - $morphType = $morph . '_type'; + $morphType = $morph . '_type'; $foreignKey = $morph . '_id'; } @@ -583,7 +583,7 @@ public function morphMany(string $model, $morph = null, string $type = ''): Morp if (is_array($morph)) { [$morphType, $foreignKey] = $morph; } else { - $morphType = $morph . '_type'; + $morphType = $morph . '_type'; $foreignKey = $morph . '_id'; } @@ -599,7 +599,7 @@ public function morphMany(string $model, $morph = null, string $type = ''): Morp */ public function morphTo($morph = null, array $alias = []): MorphTo { - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); $relation = Str::snake($trace[1]['function']); if (is_null($morph)) { @@ -610,7 +610,7 @@ public function morphTo($morph = null, array $alias = []): MorphTo if (is_array($morph)) { [$morphType, $foreignKey] = $morph; } else { - $morphType = $morph . '_type'; + $morphType = $morph . '_type'; $foreignKey = $morph . '_id'; } @@ -637,11 +637,11 @@ public function morphToMany(string $model, string $middle, $morph = null, string [$morphType, $morphKey] = $morph; } else { $morphType = $morph . '_type'; - $morphKey = $morph . '_id'; + $morphKey = $morph . '_id'; } - $model = $this->parseModel($model); - $name = Str::snake(class_basename($model)); + $model = $this->parseModel($model); + $name = Str::snake(class_basename($model)); $localKey = $localKey ?: $this->getForeignKey($name); return new MorphToMany($this, $model, $middle, $morphType, $morphKey, $localKey); @@ -667,10 +667,10 @@ public function morphByMany(string $model, string $middle, $morph = null, string [$morphType, $morphKey] = $morph; } else { $morphType = $morph . '_type'; - $morphKey = $morph . '_id'; + $morphKey = $morph . '_id'; } - $model = $this->parseModel($model); + $model = $this->parseModel($model); $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); return new MorphToMany($this, $model, $middle, $morphType, $morphKey, $foreignKey, true); @@ -817,7 +817,7 @@ protected function autoRelationInsert(): void protected function autoRelationDelete($force = false): void { foreach ($this->relationWrite as $key => $name) { - $name = is_numeric($key) ? $name : $key; + $name = is_numeric($key) ? $name : $key; $result = $this->getRelation($name, true); if ($result instanceof Model) { diff --git a/src/model/concern/SoftDelete.php b/src/model/concern/SoftDelete.php index b7b10921..3cfdb7b0 100644 --- a/src/model/concern/SoftDelete.php +++ b/src/model/concern/SoftDelete.php @@ -82,7 +82,7 @@ public function delete(): bool return false; } - $name = $this->getDeleteTimeField(); + $name = $this->getDeleteTimeField(); $force = $this->isForce(); if ($name && !$force) { diff --git a/src/model/relation/BelongsTo.php b/src/model/relation/BelongsTo.php index dfac2547..6992d2ce 100644 --- a/src/model/relation/BelongsTo.php +++ b/src/model/relation/BelongsTo.php @@ -32,12 +32,12 @@ class BelongsTo extends OneToOne */ public function __construct(Model $parent, string $model, string $foreignKey, string $localKey, string $relation = null) { - $this->parent = $parent; - $this->model = $model; + $this->parent = $parent; + $this->model = $model; $this->foreignKey = $foreignKey; - $this->localKey = $localKey; - $this->query = (new $model)->db(); - $this->relation = $relation; + $this->localKey = $localKey; + $this->query = (new $model)->db(); + $this->relation = $relation; if (get_class($parent) == $model) { $this->selfRelation = true; @@ -139,13 +139,13 @@ public function relationCount(Model $result, Closure $closure = null, string $ag */ public function has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null): Query { - $table = $this->query->getTable(); - $model = class_basename($this->parent); - $relation = class_basename($this->model); - $localKey = $this->localKey; + $table = $this->query->getTable(); + $model = class_basename($this->parent); + $relation = class_basename($this->model); + $localKey = $this->localKey; $foreignKey = $this->foreignKey; $softDelete = $this->query->getOptions('soft_delete'); - $query = $query ?: $this->parent->db()->alias($model); + $query = $query ?: $this->parent->db()->alias($model); return $query->whereExists(function ($query) use ($table, $model, $relation, $localKey, $foreignKey, $softDelete) { $query->table([$table => $relation]) @@ -168,8 +168,8 @@ public function has(string $operator = '>=', int $count = 1, string $id = '*', s */ public function hasWhere($where = [], $fields = null, string $joinType = '', Query $query = null): Query { - $table = $this->query->getTable(); - $model = class_basename($this->parent); + $table = $this->query->getTable(); + $model = class_basename($this->parent); $relation = class_basename($this->model); if (is_array($where)) { @@ -181,9 +181,9 @@ public function hasWhere($where = [], $fields = null, string $joinType = '', Que $where = $this->query; } - $fields = $this->getRelationQueryFields($fields, $model); + $fields = $this->getRelationQueryFields($fields, $model); $softDelete = $this->query->getOptions('soft_delete'); - $query = $query ?: $this->parent->db(); + $query = $query ?: $this->parent->db(); return $query->alias($model) ->field($fields) @@ -206,7 +206,7 @@ public function hasWhere($where = [], $fields = null, string $joinType = '', Que */ protected function eagerlySet(array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []): void { - $localKey = $this->localKey; + $localKey = $this->localKey; $foreignKey = $this->foreignKey; $range = []; @@ -258,7 +258,7 @@ protected function eagerlySet(array &$resultSet, string $relation, array $subRel */ protected function eagerlyOne(Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []): void { - $localKey = $this->localKey; + $localKey = $this->localKey; $foreignKey = $this->foreignKey; $this->query->removeWhereField($localKey); diff --git a/src/model/relation/BelongsToMany.php b/src/model/relation/BelongsToMany.php index 8c74f34d..d4a45888 100644 --- a/src/model/relation/BelongsToMany.php +++ b/src/model/relation/BelongsToMany.php @@ -60,14 +60,14 @@ class BelongsToMany extends Relation */ public function __construct(Model $parent, string $model, string $middle, string $foreignKey, string $localKey) { - $this->parent = $parent; - $this->model = $model; + $this->parent = $parent; + $this->model = $model; $this->foreignKey = $foreignKey; - $this->localKey = $localKey; + $this->localKey = $localKey; if (false !== strpos($middle, '\\')) { $this->pivotName = $middle; - $this->middle = class_basename($middle); + $this->middle = class_basename($middle); } else { $this->middle = $middle; } @@ -223,8 +223,8 @@ public function wherePivot($field, $op = null, $condition = null) public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []): void { $localKey = $this->localKey; - $pk = $resultSet[0]->getPk(); - $range = []; + $pk = $resultSet[0]->getPk(); + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 @@ -358,10 +358,10 @@ protected function eagerlyManyToMany(array $where, array $subRelation = [], Clos ->select(); // 组装模型数据 - $data = []; + $data = []; foreach ($list as $set) { $pivot = $this->matchPivot($set); - $key = $pivot[$this->localKey]; + $key = $pivot[$this->localKey]; if ($withLimit && isset($data[$key]) && count($data[$key]) >= $withLimit) { continue; @@ -386,7 +386,7 @@ protected function belongsToManyQuery(string $foreignKey, string $localKey, arra // 关联查询封装 if (empty($this->baseQuery)) { $tableName = $this->query->getTable(); - $table = $this->pivot->db()->getTable(); + $table = $this->pivot->db()->getTable(); if ($this->withoutField) { $this->query->withoutField($this->withoutField); @@ -459,7 +459,7 @@ public function attach($data, array $pivot = []) } else { // 保存关联表数据 $model = new $this->model; - $id = $model->insertGetId($data); + $id = $model->insertGetId($data); } } elseif (is_numeric($data) || is_string($data)) { // 根据关联表主键直接写入中间表 @@ -536,7 +536,7 @@ public function detach($data = null, bool $relationDel = false): int } // 删除中间表数据 - $pivot = []; + $pivot = []; $pivot[] = [$this->localKey, '=', $this->parent->getKey()]; if (isset($id)) { @@ -566,7 +566,7 @@ public function sync(array $ids, bool $detaching = true): array $changes = [ 'attached' => [], 'detached' => [], - 'updated' => [], + 'updated' => [], ]; $current = $this->pivot @@ -611,7 +611,7 @@ protected function baseQuery(): void { if (empty($this->baseQuery)) { $foreignKey = $this->foreignKey; - $localKey = $this->localKey; + $localKey = $this->localKey; $this->query->filter(function ($result, $options) { $this->matchPivot($result); diff --git a/src/model/relation/HasMany.php b/src/model/relation/HasMany.php index 46249555..3be704e8 100644 --- a/src/model/relation/HasMany.php +++ b/src/model/relation/HasMany.php @@ -33,11 +33,11 @@ class HasMany extends Relation */ public function __construct(Model $parent, string $model, string $foreignKey, string $localKey) { - $this->parent = $parent; - $this->model = $model; + $this->parent = $parent; + $this->model = $model; $this->foreignKey = $foreignKey; - $this->localKey = $localKey; - $this->query = (new $model)->db(); + $this->localKey = $localKey; + $this->query = (new $model)->db(); if (get_class($parent) == $model) { $this->selfRelation = true; @@ -77,7 +77,7 @@ public function getRelation(array $subRelation = [], Closure $closure = null): C public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []): void { $localKey = $this->localKey; - $range = []; + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 @@ -118,7 +118,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio $localKey = $this->localKey; if (isset($result->$localKey)) { - $pk = $result->$localKey; + $pk = $result->$localKey; $data = $this->eagerlyOneToMany([ [$this->foreignKey, '=', $pk], ], $subRelation, $closure, $cache); @@ -217,7 +217,7 @@ protected function eagerlyOneToMany(array $where, array $subRelation = [], Closu ->select(); // 组装模型数据 - $data = []; + $data = []; foreach ($list as $set) { $key = $set->$foreignKey; @@ -299,7 +299,7 @@ public function has(string $operator = '>=', int $count = 1, string $id = '*', s { $table = $this->query->getTable(); - $model = class_basename($this->parent); + $model = class_basename($this->parent); $relation = class_basename($this->model); if ('*' != $id) { @@ -307,7 +307,7 @@ public function has(string $operator = '>=', int $count = 1, string $id = '*', s } $softDelete = $this->query->getOptions('soft_delete'); - $query = $query ?: $this->parent->db()->alias($model); + $query = $query ?: $this->parent->db()->alias($model); return $query->field($model . '.*') ->join([$table => $relation], $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $joinType) @@ -329,8 +329,8 @@ public function has(string $operator = '>=', int $count = 1, string $id = '*', s */ public function hasWhere($where = [], $fields = null, string $joinType = '', Query $query = null): Query { - $table = $this->query->getTable(); - $model = class_basename($this->parent); + $table = $this->query->getTable(); + $model = class_basename($this->parent); $relation = class_basename($this->model); if (is_array($where)) { @@ -342,9 +342,9 @@ public function hasWhere($where = [], $fields = null, string $joinType = '', Que $where = $this->query; } - $fields = $this->getRelationQueryFields($fields, $model); + $fields = $this->getRelationQueryFields($fields, $model); $softDelete = $this->query->getOptions('soft_delete'); - $query = $query ?: $this->parent->db(); + $query = $query ?: $this->parent->db(); return $query->alias($model) ->group($model . '.' . $this->localKey) diff --git a/src/model/relation/HasManyThrough.php b/src/model/relation/HasManyThrough.php index 4f143993..cd9554ff 100644 --- a/src/model/relation/HasManyThrough.php +++ b/src/model/relation/HasManyThrough.php @@ -54,14 +54,14 @@ class HasManyThrough extends Relation */ public function __construct(Model $parent, string $model, string $through, string $foreignKey, string $throughKey, string $localKey, string $throughPk) { - $this->parent = $parent; - $this->model = $model; - $this->through = (new $through)->db(); + $this->parent = $parent; + $this->model = $model; + $this->through = (new $through)->db(); $this->foreignKey = $foreignKey; $this->throughKey = $throughKey; - $this->localKey = $localKey; - $this->throughPk = $throughPk; - $this->query = (new $model)->db(); + $this->localKey = $localKey; + $this->throughPk = $throughPk; + $this->query = (new $model)->db(); } /** @@ -96,13 +96,13 @@ public function getRelation(array $subRelation = [], Closure $closure = null) */ public function has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null): Query { - $model = Str::snake(class_basename($this->parent)); - $throughTable = $this->through->getTable(); - $pk = $this->throughPk; - $throughKey = $this->throughKey; - $relation = new $this->model; + $model = Str::snake(class_basename($this->parent)); + $throughTable = $this->through->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $relation = new $this->model; $relationTable = $relation->getTable(); - $softDelete = $this->query->getOptions('soft_delete'); + $softDelete = $this->query->getOptions('soft_delete'); if ('*' != $id) { $id = $relationTable . '.' . $relation->getPk(); @@ -130,11 +130,11 @@ public function has(string $operator = '>=', int $count = 1, string $id = '*', s */ public function hasWhere($where = [], $fields = null, $joinType = '', Query $query = null): Query { - $model = Str::snake(class_basename($this->parent)); + $model = Str::snake(class_basename($this->parent)); $throughTable = $this->through->getTable(); - $pk = $this->throughPk; - $throughKey = $this->throughKey; - $modelTable = (new $this->model)->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = (new $this->model)->getTable(); if (is_array($where)) { $this->getQueryWhere($where, $modelTable); @@ -145,9 +145,9 @@ public function hasWhere($where = [], $fields = null, $joinType = '', Query $que $where = $this->query; } - $fields = $this->getRelationQueryFields($fields, $model); + $fields = $this->getRelationQueryFields($fields, $model); $softDelete = $this->query->getOptions('soft_delete'); - $query = $query ?: $this->parent->db(); + $query = $query ?: $this->parent->db(); return $query->alias($model) ->join($throughTable, $throughTable . '.' . $this->foreignKey . '=' . $model . '.' . $this->localKey) @@ -172,7 +172,7 @@ public function hasWhere($where = [], $fields = null, $joinType = '', Query $que */ public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []): void { - $localKey = $this->localKey; + $localKey = $this->localKey; $foreignKey = $this->foreignKey; $range = []; @@ -215,9 +215,9 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub */ public function eagerlyResult(Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []): void { - $localKey = $this->localKey; + $localKey = $this->localKey; $foreignKey = $this->foreignKey; - $pk = $result->$localKey; + $pk = $result->$localKey; $this->query->removeWhereField($foreignKey); @@ -247,7 +247,7 @@ protected function eagerlyWhere(array $where, string $key, array $subRelation = { // 预载入关联查询 支持嵌套预载入 $throughList = $this->through->where($where)->select(); - $keys = $throughList->column($this->throughPk, $this->throughPk); + $keys = $throughList->column($this->throughPk, $this->throughPk); if ($closure) { $this->baseQuery = true; @@ -271,8 +271,8 @@ protected function eagerlyWhere(array $where, string $key, array $subRelation = ->select(); // 组装模型数据 - $data = []; - $keys = $throughList->column($this->foreignKey, $this->throughPk); + $data = []; + $keys = $throughList->column($this->foreignKey, $this->throughPk); foreach ($list as $set) { $key = $keys[$set->{$this->throughKey}]; @@ -309,11 +309,11 @@ public function relationCount(Model $result, Closure $closure = null, string $ag $closure($this->getClosureType($closure), $name); } - $alias = Str::snake(class_basename($this->model)); + $alias = Str::snake(class_basename($this->model)); $throughTable = $this->through->getTable(); - $pk = $this->throughPk; - $throughKey = $this->throughKey; - $modelTable = $this->parent->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = $this->parent->getTable(); if (false === strpos($field, '.')) { $field = $alias . '.' . $field; @@ -342,11 +342,11 @@ public function getRelationCountQuery(Closure $closure = null, string $aggregate $closure($this->getClosureType($closure), $name); } - $alias = Str::snake(class_basename($this->model)); + $alias = Str::snake(class_basename($this->model)); $throughTable = $this->through->getTable(); - $pk = $this->throughPk; - $throughKey = $this->throughKey; - $modelTable = $this->parent->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = $this->parent->getTable(); if (false === strpos($field, '.')) { $field = $alias . '.' . $field; @@ -369,11 +369,11 @@ public function getRelationCountQuery(Closure $closure = null, string $aggregate protected function baseQuery(): void { if (empty($this->baseQuery) && $this->parent->getData()) { - $alias = Str::snake(class_basename($this->model)); + $alias = Str::snake(class_basename($this->model)); $throughTable = $this->through->getTable(); - $pk = $this->throughPk; - $throughKey = $this->throughKey; - $modelTable = $this->parent->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = $this->parent->getTable(); if ($this->withoutField) { $this->query->withoutField($this->withoutField); diff --git a/src/model/relation/HasOne.php b/src/model/relation/HasOne.php index b941f6ec..6f66e627 100644 --- a/src/model/relation/HasOne.php +++ b/src/model/relation/HasOne.php @@ -31,11 +31,11 @@ class HasOne extends OneToOne */ public function __construct(Model $parent, string $model, string $foreignKey, string $localKey) { - $this->parent = $parent; - $this->model = $model; + $this->parent = $parent; + $this->model = $model; $this->foreignKey = $foreignKey; - $this->localKey = $localKey; - $this->query = (new $model)->db(); + $this->localKey = $localKey; + $this->query = (new $model)->db(); if (get_class($parent) == $model) { $this->selfRelation = true; @@ -138,13 +138,13 @@ public function relationCount(Model $result, Closure $closure = null, string $ag */ public function has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null): Query { - $table = $this->query->getTable(); - $model = class_basename($this->parent); - $relation = class_basename($this->model); - $localKey = $this->localKey; + $table = $this->query->getTable(); + $model = class_basename($this->parent); + $relation = class_basename($this->model); + $localKey = $this->localKey; $foreignKey = $this->foreignKey; $softDelete = $this->query->getOptions('soft_delete'); - $query = $query ?: $this->parent->db()->alias($model); + $query = $query ?: $this->parent->db()->alias($model); return $query->whereExists(function ($query) use ($table, $model, $relation, $localKey, $foreignKey, $softDelete) { $query->table([$table => $relation]) @@ -167,8 +167,8 @@ public function has(string $operator = '>=', int $count = 1, string $id = '*', s */ public function hasWhere($where = [], $fields = null, string $joinType = '', Query $query = null): Query { - $table = $this->query->getTable(); - $model = class_basename($this->parent); + $table = $this->query->getTable(); + $model = class_basename($this->parent); $relation = class_basename($this->model); if (is_array($where)) { @@ -180,9 +180,9 @@ public function hasWhere($where = [], $fields = null, string $joinType = '', Que $where = $this->query; } - $fields = $this->getRelationQueryFields($fields, $model); + $fields = $this->getRelationQueryFields($fields, $model); $softDelete = $this->query->getOptions('soft_delete'); - $query = $query ?: $this->parent->db(); + $query = $query ?: $this->parent->db(); return $query->alias($model) ->field($fields) @@ -205,7 +205,7 @@ public function hasWhere($where = [], $fields = null, string $joinType = '', Que */ protected function eagerlySet(array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []): void { - $localKey = $this->localKey; + $localKey = $this->localKey; $foreignKey = $this->foreignKey; $range = []; @@ -257,7 +257,7 @@ protected function eagerlySet(array &$resultSet, string $relation, array $subRel */ protected function eagerlyOne(Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []): void { - $localKey = $this->localKey; + $localKey = $this->localKey; $foreignKey = $this->foreignKey; $this->query->removeWhereField($foreignKey); diff --git a/src/model/relation/HasOneThrough.php b/src/model/relation/HasOneThrough.php index 2734c13a..3e2e0b11 100644 --- a/src/model/relation/HasOneThrough.php +++ b/src/model/relation/HasOneThrough.php @@ -58,7 +58,7 @@ public function getRelation(array $subRelation = [], Closure $closure = null) */ public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []): void { - $localKey = $this->localKey; + $localKey = $this->localKey; $foreignKey = $this->foreignKey; $range = []; @@ -105,7 +105,7 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub */ public function eagerlyResult(Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []): void { - $localKey = $this->localKey; + $localKey = $this->localKey; $foreignKey = $this->foreignKey; $this->query->removeWhereField($foreignKey); diff --git a/src/model/relation/MorphMany.php b/src/model/relation/MorphMany.php index ed0dd40a..f0095423 100644 --- a/src/model/relation/MorphMany.php +++ b/src/model/relation/MorphMany.php @@ -53,12 +53,12 @@ class MorphMany extends Relation */ public function __construct(Model $parent, string $model, string $morphKey, string $morphType, string $type) { - $this->parent = $parent; - $this->model = $model; - $this->type = $type; - $this->morphKey = $morphKey; + $this->parent = $parent; + $this->model = $model; + $this->type = $type; + $this->morphKey = $morphKey; $this->morphType = $morphType; - $this->query = (new $model)->db(); + $this->query = (new $model)->db(); } /** @@ -123,9 +123,9 @@ public function hasWhere($where = [], $fields = null, string $joinType = '', Que public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []): void { $morphType = $this->morphType; - $morphKey = $this->morphKey; - $type = $this->type; - $range = []; + $morphKey = $this->morphKey; + $type = $this->type; + $range = []; foreach ($resultSet as $result) { $pk = $result->getPk(); @@ -168,7 +168,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio $pk = $result->getPk(); if (isset($result->$pk)) { - $key = $result->$pk; + $key = $result->$pk; $data = $this->eagerlyMorphToMany([ [$this->morphKey, '=', $key], [$this->morphType, '=', $this->type], @@ -266,7 +266,7 @@ protected function eagerlyMorphToMany(array $where, array $subRelation = [], Clo $morphKey = $this->morphKey; // 组装模型数据 - $data = []; + $data = []; foreach ($list as $set) { $key = $set->$morphKey; @@ -312,7 +312,7 @@ public function make($data = []): Model // 保存关联表数据 $pk = $this->parent->getPk(); - $data[$this->morphKey] = $this->parent->$pk; + $data[$this->morphKey] = $this->parent->$pk; $data[$this->morphType] = $this->type; return new $this->model($data); diff --git a/src/model/relation/MorphOne.php b/src/model/relation/MorphOne.php index d63966ff..8040a3a5 100644 --- a/src/model/relation/MorphOne.php +++ b/src/model/relation/MorphOne.php @@ -57,12 +57,12 @@ class MorphOne extends Relation */ public function __construct(Model $parent, string $model, string $morphKey, string $morphType, string $type) { - $this->parent = $parent; - $this->model = $model; - $this->type = $type; - $this->morphKey = $morphKey; + $this->parent = $parent; + $this->model = $model; + $this->type = $type; + $this->morphKey = $morphKey; $this->morphType = $morphType; - $this->query = (new $model)->db(); + $this->query = (new $model)->db(); } /** @@ -138,9 +138,9 @@ public function hasWhere($where = [], $fields = null, string $joinType = '', Que public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []): void { $morphType = $this->morphType; - $morphKey = $this->morphKey; - $type = $this->type; - $range = []; + $morphKey = $this->morphKey; + $type = $this->type; + $range = []; foreach ($resultSet as $result) { $pk = $result->getPk(); @@ -192,7 +192,7 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio $pk = $result->getPk(); if (isset($result->$pk)) { - $pk = $result->$pk; + $pk = $result->$pk; $data = $this->eagerlyMorphToOne([ [$this->morphKey, '=', $pk], [$this->morphType, '=', $this->type], @@ -281,7 +281,7 @@ public function make($data = []): Model // 保存关联表数据 $pk = $this->parent->getPk(); - $data[$this->morphKey] = $this->parent->$pk; + $data[$this->morphKey] = $this->parent->$pk; $data[$this->morphType] = $this->type; return new $this->model($data); @@ -339,7 +339,7 @@ public function getBindAttr(): array protected function bindAttr(Model $result, Model $model = null): void { foreach ($this->bindAttr as $key => $attr) { - $key = is_numeric($key) ? $attr : $key; + $key = is_numeric($key) ? $attr : $key; $value = $result->getOrigin($key); if (!is_null($value)) { diff --git a/src/model/relation/MorphTo.php b/src/model/relation/MorphTo.php index d24c0826..27afcf41 100644 --- a/src/model/relation/MorphTo.php +++ b/src/model/relation/MorphTo.php @@ -60,11 +60,11 @@ class MorphTo extends Relation */ public function __construct(Model $parent, string $morphType, string $morphKey, array $alias = [], string $relation = null) { - $this->parent = $parent; + $this->parent = $parent; $this->morphType = $morphType; - $this->morphKey = $morphKey; - $this->alias = $alias; - $this->relation = $relation; + $this->morphKey = $morphKey; + $this->alias = $alias; + $this->relation = $relation; } /** @@ -75,7 +75,7 @@ public function __construct(Model $parent, string $morphType, string $morphKey, public function getModel(): Model { $morphType = $this->morphType; - $model = $this->parseModel($this->parent->$morphType); + $model = $this->parseModel($this->parent->$morphType); return (new $model); } @@ -89,7 +89,7 @@ public function getModel(): Model */ public function getRelation(array $subRelation = [], Closure $closure = null) { - $morphKey = $this->morphKey; + $morphKey = $this->morphKey; $morphType = $this->morphType; // 多态模型 @@ -218,9 +218,9 @@ public function removeOption(string $option = '') */ public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []): void { - $morphKey = $this->morphKey; + $morphKey = $this->morphKey; $morphType = $this->morphType; - $range = []; + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 @@ -234,11 +234,11 @@ public function eagerlyResultSet(array &$resultSet, string $relation, array $sub foreach ($range as $key => $val) { // 多态类型映射 $model = $this->parseModel($key); - $obj = new $model; + $obj = new $model; if (!\is_null($closure)) { $obj = $closure($obj); } - $pk = $obj->getPk(); + $pk = $obj->getPk(); $list = $obj->with($subRelation) ->cache($cache[0] ?? false, $cache[1] ?? null, $cache[2] ?? null) ->select($val); @@ -337,9 +337,9 @@ protected function eagerlyMorphToOne(string $model, string $relation, Model $res */ public function associate(Model $model, string $type = ''): Model { - $morphKey = $this->morphKey; + $morphKey = $this->morphKey; $morphType = $this->morphType; - $pk = $model->getPk(); + $pk = $model->getPk(); $this->parent->setAttr($morphKey, $model->$pk); $this->parent->setAttr($morphType, $type ?: get_class($model)); @@ -355,7 +355,7 @@ public function associate(Model $model, string $type = ''): Model */ public function dissociate(): Model { - $morphKey = $this->morphKey; + $morphKey = $this->morphKey; $morphType = $this->morphType; $this->parent->setAttr($morphKey, null); diff --git a/src/model/relation/MorphToMany.php b/src/model/relation/MorphToMany.php index c9d8583d..88edf72f 100644 --- a/src/model/relation/MorphToMany.php +++ b/src/model/relation/MorphToMany.php @@ -62,15 +62,15 @@ class MorphToMany extends BelongsToMany */ public function __construct(Model $parent, string $model, string $middle, string $morphType, string $morphKey, string $localKey, bool $inverse = false) { - $this->morphType = $morphType; - $this->inverse = $inverse; + $this->morphType = $morphType; + $this->inverse = $inverse; $this->morphClass = $inverse ? $model : get_class($parent); if (isset(static::$morphMap[$this->morphClass])) { $this->morphClass = static::$morphMap[$this->morphClass]; } $foreignKey = $inverse ? $morphKey : $localKey; - $localKey = $inverse ? $localKey : $morphKey; + $localKey = $inverse ? $localKey : $morphKey; parent::__construct($parent, $model, $middle, $foreignKey, $localKey); } @@ -87,7 +87,7 @@ public function __construct(Model $parent, string $model, string $middle, string */ public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []): void { - $pk = $resultSet[0]->getPk(); + $pk = $resultSet[0]->getPk(); $range = []; foreach ($resultSet as $result) { @@ -209,14 +209,14 @@ protected function belongsToManyQuery(string $foreignKey, string $localKey, arra { // 关联查询封装 $tableName = $this->query->getTable(); - $table = $this->pivot->db()->getTable(); + $table = $this->pivot->db()->getTable(); if ($this->withoutField) { $this->query->withoutField($this->withoutField); } $fields = $this->getQueryFields($tableName); - $query = $this->query + $query = $this->query ->field($fields) ->tableField(true, $table, 'pivot', 'pivot__'); @@ -256,7 +256,7 @@ protected function eagerlyManyToMany(array $where, array $subRelation = [], Clos ->select(); // 组装模型数据 - $data = []; + $data = []; foreach ($list as $set) { $pivot = []; foreach ($set->getData() as $key => $val) { @@ -298,7 +298,7 @@ public function attach($data, array $pivot = []) } else { // 保存关联表数据 $model = new $this->model; - $id = $model->insertGetId($data); + $id = $model->insertGetId($data); } } elseif (is_numeric($data) || is_string($data)) { // 根据关联表主键直接写入中间表 @@ -310,9 +310,9 @@ public function attach($data, array $pivot = []) if (!empty($id)) { // 保存中间表数据 - $pivot[$this->localKey] = $this->parent->getKey(); + $pivot[$this->localKey] = $this->parent->getKey(); $pivot[$this->morphType] = $this->morphClass; - $ids = (array) $id; + $ids = (array) $id; $result = []; @@ -412,7 +412,7 @@ public function sync(array $ids, bool $detaching = true): array $changes = [ 'attached' => [], 'detached' => [], - 'updated' => [], + 'updated' => [], ]; $current = $this->pivot @@ -458,7 +458,7 @@ protected function baseQuery(): void { if (empty($this->baseQuery)) { $foreignKey = $this->foreignKey; - $localKey = $this->localKey; + $localKey = $this->localKey; // 关联查询 $this->belongsToManyQuery($foreignKey, $localKey, [ diff --git a/src/model/relation/OneToOne.php b/src/model/relation/OneToOne.php index d62692aa..95d42d8a 100644 --- a/src/model/relation/OneToOne.php +++ b/src/model/relation/OneToOne.php @@ -86,7 +86,7 @@ public function eagerly(Query $query, string $relation, $field = true, string $j // 预载入封装 $joinTable = $this->query->getTable(); $joinAlias = $relation; - $joinType = $joinType ?: $this->joinType; + $joinType = $joinType ?: $this->joinType; $query->via($joinAlias); @@ -303,7 +303,7 @@ protected function match(string $model, string $relation, Model $result): void protected function bindAttr(Model $result, Model $model = null): void { foreach ($this->bindAttr as $key => $attr) { - $key = is_numeric($key) ? $attr : $key; + $key = is_numeric($key) ? $attr : $key; $value = $result->getOrigin($key); if (!is_null($value)) { diff --git a/src/paginator/driver/Bootstrap.php b/src/paginator/driver/Bootstrap.php index 6d55c394..1ffb89c6 100644 --- a/src/paginator/driver/Bootstrap.php +++ b/src/paginator/driver/Bootstrap.php @@ -65,26 +65,26 @@ protected function getLinks(): string } $block = [ - 'first' => null, + 'first' => null, 'slider' => null, - 'last' => null, + 'last' => null, ]; - $side = 3; + $side = 3; $window = $side * 2; if ($this->lastPage < $window + 6) { $block['first'] = $this->getUrlRange(1, $this->lastPage); } elseif ($this->currentPage <= $window) { $block['first'] = $this->getUrlRange(1, $window + 2); - $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); + $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); } elseif ($this->currentPage > ($this->lastPage - $window)) { $block['first'] = $this->getUrlRange(1, 2); - $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage); + $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage); } else { - $block['first'] = $this->getUrlRange(1, 2); + $block['first'] = $this->getUrlRange(1, 2); $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side); - $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); + $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); } $html = ''; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b953ceb5..957f118d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -7,12 +7,12 @@ // 数据库配置信息设置(全局有效) Db::setConfig([ // 默认数据连接标识 - 'default' => 'mysql', + 'default' => 'mysql', // 数据库连接信息 'connections' => [ 'mysql' => [ // 数据库类型 - 'type' => 'mysql', + 'type' => 'mysql', // 主机地址 'hostname' => getenv('TESTS_DB_MYSQL_HOSTNAME'), // 数据库名 @@ -22,19 +22,19 @@ // 密码 'password' => getenv('TESTS_DB_MYSQL_PASSWORD'), // 数据库编码默认采用utf8 - 'charset' => 'utf8', + 'charset' => 'utf8', // 数据库表前缀 - 'prefix' => 'test_', + 'prefix' => 'test_', // 是否需要断线重连 'break_reconnect' => false, // 断线标识字符串 'break_match_str' => [], // 数据库调试模式 - 'debug' => false, + 'debug' => false, ], 'mysql_manage' => [ // 数据库类型 - 'type' => 'mysql', + 'type' => 'mysql', // 主机地址 'hostname' => getenv('TESTS_DB_MYSQL_HOSTNAME'), // 数据库名 @@ -44,11 +44,11 @@ // 密码 'password' => getenv('TESTS_DB_MYSQL_PASSWORD'), // 数据库编码默认采用utf8 - 'charset' => 'utf8', + 'charset' => 'utf8', // 数据库表前缀 - 'prefix' => 'test_', + 'prefix' => 'test_', // 数据库调试模式 - 'debug' => false, + 'debug' => false, ], ], ]);