From fc681a117cc1e9b5be920de38e89d665e605a164 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 11 Jan 2024 02:47:57 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Model.php | 24 ++--- src/Paginator.php | 32 +++---- src/db/BaseBuilder.php | 18 ++-- src/db/BaseQuery.php | 82 ++++++++--------- src/db/Builder.php | 16 ++-- src/db/Connection.php | 6 +- src/db/Fetch.php | 4 +- src/db/Mongo.php | 6 +- src/db/PDOConnection.php | 84 ++++++++--------- src/db/builder/Mongo.php | 14 +-- src/db/builder/Mysql.php | 32 +++---- src/db/concern/TimeFieldQuery.php | 14 +-- src/db/connector/Mongo.php | 128 +++++++++++++------------- src/db/connector/Mysql.php | 16 ++-- src/db/connector/Oracle.php | 16 ++-- src/db/connector/Pgsql.php | 22 ++--- src/db/connector/Sqlite.php | 14 +-- src/db/connector/Sqlsrv.php | 24 ++--- src/db/exception/DbException.php | 4 +- src/db/exception/PDOException.php | 4 +- src/model/Pivot.php | 4 +- src/model/concern/Attribute.php | 52 +++++------ src/model/concern/Conversion.php | 12 +-- src/model/concern/OptimLock.php | 4 +- src/model/concern/RelationShip.php | 122 ++++++++++++------------ src/model/relation/BelongsTo.php | 42 ++++----- src/model/relation/BelongsToMany.php | 16 ++-- src/model/relation/HasMany.php | 30 +++--- src/model/relation/HasManyThrough.php | 78 ++++++++-------- src/model/relation/HasOne.php | 36 ++++---- src/model/relation/HasOneThrough.php | 6 +- src/model/relation/MorphMany.php | 26 +++--- src/model/relation/MorphOne.php | 22 ++--- src/model/relation/MorphTo.php | 34 +++---- src/model/relation/MorphToMany.php | 20 ++-- src/model/relation/OneToOne.php | 10 +- src/paginator/driver/Bootstrap.php | 4 +- tests/bootstrap.php | 18 ++-- 38 files changed, 548 insertions(+), 548 deletions(-) diff --git a/src/Model.php b/src/Model.php index 714b341b..30f42f85 100644 --- a/src/Model.php +++ b/src/Model.php @@ -492,9 +492,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->origin = $this->data; - $this->get = []; + $this->data = $this->db()->find($this->getKey())->getData(); + $this->origin = $this->data; + $this->get = []; if ($relation) { $this->relation = []; @@ -571,8 +571,8 @@ public function save(array|object $data = [], string $sequence = null): bool $this->trigger('AfterWrite'); // 重新记录原始数据 - $this->origin = $this->data; - $this->get = []; + $this->origin = $this->data; + $this->get = []; return true; } @@ -663,9 +663,9 @@ protected function updateData(): bool $db = $this->db(); $db->transaction(function () use ($data, $allowFields, $db) { - $this->key = null; - $where = $this->getWhere(); - $result = $db->where($where) + $this->key = null; + $where = $this->getWhere(); + $result = $db->where($where) ->strict(false) ->cache(true) ->setOption('key', $this->key) @@ -705,7 +705,7 @@ protected function insertData(string $sequence = null): bool // 时间戳自动写入 if ($this->autoWriteTimestamp) { if ($this->createTime && !array_key_exists($this->createTime, $data)) { - $data[$this->createTime] = $this->autoWriteTimestamp(); + $data[$this->createTime] = $this->autoWriteTimestamp(); $this->data[$this->createTime] = $data[$this->createTime]; } @@ -835,8 +835,8 @@ public function delete(): bool } // 读取更新条件 - $where = $this->getWhere(); - $db = $this->db(); + $where = $this->getWhere(); + $db = $this->db(); $db->transaction(function () use ($where, $db) { // 删除当前模型数据 @@ -850,7 +850,7 @@ public function delete(): bool $this->trigger('AfterDelete'); - $this->exists = false; + $this->exists = false; return true; } diff --git a/src/Paginator.php b/src/Paginator.php index 971b052d..27e067b4 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -86,8 +86,8 @@ abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, J */ protected $options = [ 'var_page' => 'page', - 'path' => '/', - 'query' => [], + 'path' => '/', + 'query' => [], 'fragment' => '', ]; @@ -116,7 +116,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) { @@ -124,14 +124,14 @@ 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->currentPage = $this->setCurrentPage($currentPage); + $this->hasMore = count($items) > ($this->listRows); + $items = $items->slice(0, $this->listRows); } else { - $this->total = $total; - $this->lastPage = (int) ceil($total / $listRows); - $this->currentPage = $this->setCurrentPage($currentPage); - $this->hasMore = $this->currentPage < $this->lastPage; + $this->total = $total; + $this->lastPage = (int) ceil($total / $listRows); + $this->currentPage = $this->setCurrentPage($currentPage); + $this->hasMore = $this->currentPage < $this->lastPage; } $this->items = $items; } @@ -184,10 +184,10 @@ protected function url(int $page): string if (!str_contains($this->options['path'], '[PAGE]')) { $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) { @@ -526,11 +526,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/BaseBuilder.php b/src/db/BaseBuilder.php index 6cc48fc3..0a250b82 100644 --- a/src/db/BaseBuilder.php +++ b/src/db/BaseBuilder.php @@ -42,16 +42,16 @@ abstract class BaseBuilder * @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'], ]; /** diff --git a/src/db/BaseQuery.php b/src/db/BaseQuery.php index 6f9d823c..114247ce 100644 --- a/src/db/BaseQuery.php +++ b/src/db/BaseQuery.php @@ -84,8 +84,8 @@ abstract class BaseQuery */ public function __construct(ConnectionInterface $connection) { - $this->connection = $connection; - $this->prefix = $this->connection->getConfig('prefix'); + $this->connection = $connection; + $this->prefix = $this->connection->getConfig('prefix'); } /** @@ -418,7 +418,7 @@ public function field(string|array|Raw|bool $field) if (true === $field) { // 获取全部字段 $fields = $this->getTableFields(); - $field = $fields ?: ['*']; + $field = $fields ?: ['*']; } if (isset($this->options['field'])) { @@ -449,7 +449,7 @@ public function withoutField(array|string $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); @@ -483,7 +483,7 @@ public function tableField(string|array|bool $field, string $tableName, string $ if (true === $field) { // 获取全部字段 $fields = $this->getTableFields($tableName); - $field = $fields ?: ['*']; + $field = $fields ?: ['*']; } // 添加统一的前缀 @@ -530,8 +530,8 @@ public function data(array $data) public function removeOption(string $option = '') { if ('' === $option) { - $this->options = []; - $this->bind = []; + $this->options = []; + $this->bind = []; } elseif (isset($this->options[$option])) { unset($this->options[$option]); } @@ -710,27 +710,27 @@ public function order(string|array|Raw $field, string $order = '') public function paginate(int|array $listRows = null, int|bool $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); - $listRows = intval($config['list_rows']); + $config = array_merge($defaultConfig, $listRows); + $listRows = intval($config['list_rows']); } else { - $config = $defaultConfig; - $listRows = intval($listRows ?: $config['list_rows']); + $config = $defaultConfig; + $listRows = intval($listRows ?: $config['list_rows']); } - $page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']); - $page = max($page, 1); + $page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']); + $page = max($page, 1); $config['path'] = $config['path'] ?? Paginator::getCurrentPath(); if (!isset($total) && !$simple) { @@ -738,8 +738,8 @@ public function paginate(int|array $listRows = null, int|bool $simple = false): unset($this->options['order'], $this->options['cache'], $this->options['limit'], $this->options['page'], $this->options['field']); - $bind = $this->bind; - $total = $this->count(); + $bind = $this->bind; + $total = $this->count(); if ($total > 0) { $results = $this->options($options)->bind($bind)->page($page, $listRows)->select(); } else { @@ -750,10 +750,10 @@ public function paginate(int|array $listRows = null, int|bool $simple = false): } } } elseif ($simple) { - $results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select(); - $total = null; + $results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select(); + $total = null; } else { - $results = $this->page($page, $listRows)->select(); + $results = $this->page($page, $listRows)->select(); } $this->removeOption('limit'); @@ -776,21 +776,21 @@ public function paginate(int|array $listRows = null, int|bool $simple = false): public function paginateX(int|array $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; - $listRows = is_int($listRows) ? $listRows : (int) $config['list_rows']; - $page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']); - $page = max($page, 1); + $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 = max($page, 1); $config['path'] = $config['path'] ?? Paginator::getCurrentPath(); - $key = $key ?: $this->getPk(); - $options = $this->getOptions(); + $key = $key ?: $this->getPk(); + $options = $this->getOptions(); if (is_null($sort)) { $order = $options['order'] ?? ''; @@ -871,7 +871,7 @@ public function more(int $limit, int|string $lastId = null, string $key = null, $result->first(); return [ - 'data' => $result, + 'data' => $result, 'lastId' => $last ? $last[$key] : null, ]; } @@ -903,10 +903,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; } @@ -921,7 +921,7 @@ public function cache($key = true, $expire = null, $tag = null) */ public function cacheAlways($key = true, $expire = null, $tag = null) { - $this->options['cache_always'] = true; + $this->options['cache_always'] = true; return $this->cache($key, $expire, $tag); } @@ -936,7 +936,7 @@ public function cacheAlways($key = true, $expire = null, $tag = null) */ public function cacheForce($key = true, $expire = null, $tag = null) { - $this->options['force_cache'] = true; + $this->options['force_cache'] = true; return $this->cache($key, $expire, $tag); } @@ -1031,8 +1031,8 @@ public function sequence(string $sequence = null) */ public function json(array $json = [], bool $assoc = false) { - $this->options['json'] = $json; - $this->options['json_assoc'] = $assoc; + $this->options['json'] = $json; + $this->options['json_assoc'] = $assoc; return $this; } @@ -1423,9 +1423,9 @@ public function parseOptions(): array // 根据页数计算limit [$page, $listRows] = $options['page']; - $page = $page > 0 ? $page : 1; - $listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20); - $offset = $listRows * ($page - 1); + $page = $page > 0 ? $page : 1; + $listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20); + $offset = $listRows * ($page - 1); $options['limit'] = $offset . ',' . $listRows; } diff --git a/src/db/Builder.php b/src/db/Builder.php index da3fdc0e..e12cf3d5 100644 --- a/src/db/Builder.php +++ b/src/db/Builder.php @@ -82,9 +82,9 @@ protected function parseData(Query $query, array $data = [], array $fields = [], } } elseif (is_array($val) && !empty($val) && is_string($val[0])) { if (in_array(strtoupper($val[0]), ['INC', 'DEC'])) { - $result[$item] = match (strtoupper($val[0])) { - 'INC' => $item . ' + ' . floatval($val[1]), - 'DEC' => $item . ' - ' . floatval($val[1]), + $result[$item] = match (strtoupper($val[0])) { + 'INC' => $item . ' + ' . floatval($val[1]), + 'DEC' => $item . ' - ' . floatval($val[1]), }; } } elseif (is_scalar($val)) { @@ -407,8 +407,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 (Connection::PARAM_STR == $bindType) { @@ -565,8 +565,8 @@ protected function parseOrder(Query $query, array $order): string */ protected function parseRaw(Query $query, Raw $raw): string { - $sql = $raw->getValue(); - $bind = $raw->getBind(); + $sql = $raw->getValue(); + $bind = $raw->getBind(); if ($bind) { $query->bindParams($sql, $bind); @@ -740,7 +740,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 03a3d27f..ff343165 100644 --- a/src/db/Connection.php +++ b/src/db/Connection.php @@ -21,9 +21,9 @@ */ abstract class Connection implements ConnectionInterface { - const PARAM_INT = 1; - const PARAM_STR = 2; - const PARAM_BOOL = 5; + const PARAM_INT = 1; + const PARAM_STR = 2; + const PARAM_BOOL = 5; const PARAM_FLOAT = 21; /** diff --git a/src/db/Fetch.php b/src/db/Fetch.php index 0d518035..d5fb5d73 100644 --- a/src/db/Fetch.php +++ b/src/db/Fetch.php @@ -43,8 +43,8 @@ class Fetch */ public function __construct(protected Query $query) { - $this->connection = $query->getConnection(); - $this->builder = $this->connection->getBuilder(); + $this->connection = $query->getConnection(); + $this->builder = $this->connection->getBuilder(); } /** diff --git a/src/db/Mongo.php b/src/db/Mongo.php index 1f874182..15d5f166 100644 --- a/src/db/Mongo.php +++ b/src/db/Mongo.php @@ -574,9 +574,9 @@ public function paginate($listRows = null, $simple = false): Paginator } $defaultConfig = [ - 'query' => [], //url额外参数 - 'fragment' => '', //url锚点 - 'var_page' => 'page', //分页变量 + 'query' => [], //url额外参数 + 'fragment' => '', //url锚点 + 'var_page' => 'page', //分页变量 'list_rows' => 15, //每页数量 ]; diff --git a/src/db/PDOConnection.php b/src/db/PDOConnection.php index cfdcce72..9fa21efd 100644 --- a/src/db/PDOConnection.php +++ b/src/db/PDOConnection.php @@ -39,45 +39,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, // 断线标识字符串 @@ -148,11 +148,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, ]; /** @@ -161,14 +161,14 @@ abstract class PDOConnection extends Connection * @var array */ protected $bindType = [ - 'string' => self::PARAM_STR, - 'str' => self::PARAM_STR, - 'integer' => self::PARAM_INT, - 'int' => self::PARAM_INT, - 'boolean' => self::PARAM_BOOL, - 'bool' => self::PARAM_BOOL, - 'float' => self::PARAM_FLOAT, - 'datetime' => self::PARAM_STR, + 'string' => self::PARAM_STR, + 'str' => self::PARAM_STR, + 'integer' => self::PARAM_INT, + 'int' => self::PARAM_INT, + 'boolean' => self::PARAM_BOOL, + 'bool' => self::PARAM_BOOL, + 'float' => self::PARAM_FLOAT, + 'datetime' => self::PARAM_STR, 'timestamp' => self::PARAM_STR, ]; @@ -276,10 +276,10 @@ public function fieldCase(array $info): array { // 字段大小写转换 return match ($this->attrCase) { - PDO::CASE_LOWER => array_change_key_case($info), - PDO::CASE_UPPER => array_change_key_case($info, CASE_UPPER), - PDO::CASE_NATURAL => $info, - default => $info, + PDO::CASE_LOWER => array_change_key_case($info), + PDO::CASE_UPPER => array_change_key_case($info, CASE_UPPER), + PDO::CASE_NATURAL => $info, + default => $info, }; } @@ -389,10 +389,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, ]; } @@ -891,8 +891,8 @@ protected function pdoExecute(BaseQuery $query, string $sql, bool $origin = fals */ protected function queryPDOStatement(BaseQuery $query, string $sql): PDOStatement { - $options = $query->getOptions(); - $bind = $query->getBind(); + $options = $query->getOptions(); + $bind = $query->getBind(); $master = !empty($options['master']); $procedure = !empty($options['procedure']) || in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']); diff --git a/src/db/builder/Mongo.php b/src/db/builder/Mongo.php index ad691c42..8ea338a2 100644 --- a/src/db/builder/Mongo.php +++ b/src/db/builder/Mongo.php @@ -552,10 +552,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) { @@ -601,10 +601,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) { @@ -633,7 +633,7 @@ public function distinct(Query $query, $field): Command $cmd = [ 'distinct' => $options['table'], - 'key' => $field, + 'key' => $field, ]; if (!empty($options['where'])) { diff --git a/src/db/builder/Mysql.php b/src/db/builder/Mysql.php index 96aad75f..89aff15c 100644 --- a/src/db/builder/Mysql.php +++ b/src/db/builder/Mysql.php @@ -30,18 +30,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'], ]; /** @@ -161,8 +161,8 @@ public function insert(Query $query): string */ public function insertAll(Query $query, array $dataSet): string { - $options = $query->getOptions(); - $bind = $query->getFieldsBindType(); + $options = $query->getOptions(); + $bind = $query->getFieldsBindType(); // 获取合法的字段 if (empty($options['field']) || '*' == $options['field']) { @@ -215,7 +215,7 @@ public function insertAll(Query $query, array $dataSet): string public function insertAllByKeys(Query $query, array $keys, array $datas): string { $options = $query->getOptions(); - $bind = $query->getFieldsBindType(); + $bind = $query->getFieldsBindType(); $fields = []; $values = []; @@ -259,8 +259,8 @@ public function insertAllByKeys(Query $query, array $keys, array $datas): string */ public function update(Query $query): string { - $options = $query->getOptions(); - $data = $this->parseData($query, $options['data']); + $options = $query->getOptions(); + $data = $this->parseData($query, $options['data']); if (empty($data)) { return ''; diff --git a/src/db/concern/TimeFieldQuery.php b/src/db/concern/TimeFieldQuery.php index b5830fef..be41d5fd 100644 --- a/src/db/concern/TimeFieldQuery.php +++ b/src/db/concern/TimeFieldQuery.php @@ -24,14 +24,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'], ]; /** diff --git a/src/db/connector/Mongo.php b/src/db/connector/Mongo.php index bc914322..54bc46a6 100644 --- a/src/db/connector/Mongo.php +++ b/src/db/connector/Mongo.php @@ -55,57 +55,57 @@ 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, // Builder类 - 'builder' => '', + 'builder' => '', // Query类 - 'query' => '', + 'query' => '', // 自动写入时间戳字段 - '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'], ]; /** @@ -222,7 +222,7 @@ public function cursor($query) // 生成MongoQuery对象 $mongoQuery = $this->builder->select($query); - $master = (bool) $query->getOptions('master'); + $master = (bool) $query->getOptions('master'); // 执行查询操作 return $this->getCursor($query, $mongoQuery, $master); @@ -247,8 +247,8 @@ public function getCursor(BaseQuery $query, $mongoQuery, bool $master = false): $this->initConnect($master); $this->db->updateQueryTimes(); - $options = $query->getOptions(); - $namespace = $options['table']; + $options = $query->getOptions(); + $namespace = $options['table']; if (!str_contains($namespace, '.')) { $namespace = $this->dbName . '.' . $namespace; @@ -269,7 +269,7 @@ public function getCursor(BaseQuery $query, $mongoQuery, bool $master = false): 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); @@ -337,8 +337,8 @@ protected function mongoQuery(BaseQuery $query, $mongoQuery): array if ($query->getOptions('cache')) { // 检查查询缓存 - $cacheItem = $this->parseCache($query, $query->getOptions('cache')); - $key = $cacheItem->getKey(); + $cacheItem = $this->parseCache($query, $query->getOptions('cache')); + $key = $cacheItem->getKey(); if ($this->cache->has($key)) { return $this->cache->get($key); @@ -382,8 +382,8 @@ protected function mongoExecute(BaseQuery $query, BulkWrite $bulk) $this->initConnect(true); $this->db->updateQueryTimes(); - $options = $query->getOptions(); - $namespace = $options['table']; + $options = $query->getOptions(); + $namespace = $options['table']; if (!str_contains($namespace, '.')) { $namespace = $this->dbName . '.' . $namespace; } @@ -398,7 +398,7 @@ protected function mongoExecute(BaseQuery $query, BulkWrite $bulk) if ($session = $this->getSession()) { $writeResult = $this->mongo->executeBulkWrite($namespace, $bulk, [ - 'session' => $session, + 'session' => $session, 'writeConcern' => is_null($writeConcern) ? new WriteConcern(1) : $writeConcern, ]); } else { @@ -414,9 +414,9 @@ protected function mongoExecute(BaseQuery $query, BulkWrite $bulk) if ($query->getOptions('cache')) { // 清理缓存数据 - $cacheItem = $this->parseCache($query, $query->getOptions('cache')); - $key = $cacheItem->getKey(); - $tag = $cacheItem->getTag(); + $cacheItem = $this->parseCache($query, $query->getOptions('cache')); + $key = $cacheItem->getKey(); + $tag = $cacheItem->getTag(); if (isset($key) && $this->cache->has($key)) { $this->cache->delete($key); @@ -460,7 +460,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); @@ -594,11 +594,11 @@ public function getLastSql(): string */ public function close() { - $this->mongo = null; - $this->cursor = null; - $this->linkRead = null; - $this->linkWrite = null; - $this->links = []; + $this->mongo = null; + $this->cursor = null; + $this->linkRead = null; + $this->linkWrite = null; + $this->links = []; } /** @@ -685,9 +685,9 @@ protected function multiConnect(bool $master = false): Manager */ public function replicaSetConnect(): Manager { - $this->dbName = $this->config['database']; - $this->typeMap = $this->config['type_map']; - $startTime = microtime(true); + $this->dbName = $this->config['database']; + $this->typeMap = $this->config['type_map']; + $startTime = microtime(true); $this->config['params']['replicaSet'] = $this->config['database']; @@ -744,17 +744,17 @@ public function insert(BaseQuery $query, bool $getLastInsID = false) } // 生成bulk对象 - $bulk = $this->builder->insert($query); - $writeResult = $this->mongoExecute($query, $bulk); - $result = $writeResult->getInsertedCount(); + $bulk = $this->builder->insert($query); + $writeResult = $this->mongoExecute($query, $bulk); + $result = $writeResult->getInsertedCount(); if ($result) { - $data = $options['data']; - $lastInsId = $this->getLastInsID($query); + $data = $options['data']; + $lastInsId = $this->getLastInsID($query); if ($lastInsId) { - $pk = $query->getPk(); - $data[$pk] = $lastInsId; + $pk = $query->getPk(); + $data[$pk] = $lastInsId; } $query->setOption('data', $data); @@ -818,8 +818,8 @@ public function insertAll(BaseQuery $query, array $dataSet = []): int } // 生成bulkWrite对象 - $bulk = $this->builder->insertAll($query, $dataSet); - $writeResult = $this->mongoExecute($query, $bulk); + $bulk = $this->builder->insertAll($query, $dataSet); + $writeResult = $this->mongoExecute($query, $bulk); return $writeResult->getInsertedCount(); } @@ -843,9 +843,9 @@ public function update(BaseQuery $query): int $query->parseOptions(); // 生成bulkWrite对象 - $bulk = $this->builder->update($query); - $writeResult = $this->mongoExecute($query, $bulk); - $result = $writeResult->getModifiedCount(); + $bulk = $this->builder->update($query); + $writeResult = $this->mongoExecute($query, $bulk); + $result = $writeResult->getModifiedCount(); if ($result) { $this->db->trigger('after_update', $query); @@ -874,10 +874,10 @@ public function delete(BaseQuery $query): int $query->parseOptions(); // 生成bulkWrite对象 - $bulk = $this->builder->delete($query); + $bulk = $this->builder->delete($query); // 执行操作 - $writeResult = $this->mongoExecute($query, $bulk); - $result = $writeResult->getDeletedCount(); + $writeResult = $this->mongoExecute($query, $bulk); + $result = $writeResult->getDeletedCount(); if ($result) { $this->db->trigger('after_delete', $query); @@ -983,7 +983,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; @@ -1028,8 +1028,8 @@ public function column(BaseQuery $query, string|array $field, string $key = ''): if (!empty($options['cache'])) { // 判断查询缓存 - $cacheItem = $this->parseCache($query, $options['cache']); - $key = $cacheItem->getKey(); + $cacheItem = $this->parseCache($query, $options['cache']); + $key = $cacheItem->getKey(); if ($this->cache->has($key)) { return $this->cache->get($key); diff --git a/src/db/connector/Mysql.php b/src/db/connector/Mysql.php index f18da995..8e8ce6d0 100644 --- a/src/db/connector/Mysql.php +++ b/src/db/connector/Mysql.php @@ -64,18 +64,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', @@ -97,10 +97,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 3877b973..fd614c7f 100644 --- a/src/db/connector/Oracle.php +++ b/src/db/connector/Oracle.php @@ -55,18 +55,18 @@ 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 (+)"; - $pdo = $this->getPDOStatement($sql); + $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); $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'], @@ -87,10 +87,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); diff --git a/src/db/connector/Pgsql.php b/src/db/connector/Pgsql.php index 023a886f..31df05e4 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, ]; @@ -60,18 +60,18 @@ 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 . '\');'; - $pdo = $this->getPDOStatement($sql); + $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); $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']), @@ -92,10 +92,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 5a429a34..8b334160 100644 --- a/src/db/connector/Sqlite.php +++ b/src/db/connector/Sqlite.php @@ -43,18 +43,18 @@ public function getFields(string $tableName): array { [$tableName] = explode(' ', $tableName); - $sql = 'PRAGMA table_info( \'' . $tableName . '\' )'; - $pdo = $this->getPDOStatement($sql); + $sql = 'PRAGMA table_info( \'' . $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['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'], @@ -79,9 +79,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 b925891a..6dc337a7 100644 --- a/src/db/connector/Sqlsrv.php +++ b/src/db/connector/Sqlsrv.php @@ -26,9 +26,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, ]; @@ -66,24 +66,24 @@ public function getFields(string $tableName): array [$tableName] = explode(' ', $tableName); str_contains($tableName, '.') && $tableName = substr($tableName, strpos($tableName, '.') + 1); - $sql = "SELECT column_name, data_type, column_default, is_nullable + $sql = "SELECT column_name, data_type, column_default, is_nullable FROM information_schema.tables AS t JOIN information_schema.columns AS c ON t.table_catalog = c.table_catalog AND t.table_schema = c.table_schema 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, @@ -92,8 +92,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) { @@ -117,9 +117,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/DbException.php b/src/db/exception/DbException.php index 40b8443e..f8cdf05e 100644 --- a/src/db/exception/DbException.php +++ b/src/db/exception/DbException.php @@ -34,9 +34,9 @@ public function __construct(string $message, array $config = [], string $sql = ' $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/PDOException.php b/src/db/exception/PDOException.php index 247e6a78..2829d9a9 100644 --- a/src/db/exception/PDOException.php +++ b/src/db/exception/PDOException.php @@ -34,8 +34,8 @@ public function __construct(\PDOException $exception, array $config = [], string if (!empty($error)) { $this->setData('PDO Error Info', [ - 'SQLSTATE' => $error[0], - 'Driver Error Code' => $error[1] ?? 0, + 'SQLSTATE' => $error[0], + 'Driver Error Code' => $error[1] ?? 0, 'Driver Error Message' => $error[2] ?? '', ]); } diff --git a/src/model/Pivot.php b/src/model/Pivot.php index 07144e89..812ec94a 100644 --- a/src/model/Pivot.php +++ b/src/model/Pivot.php @@ -65,8 +65,8 @@ public function newInstance(array $data = [], $where = null, array $options = [] { $model = parent::newInstance($data, $where, $options); - $model->parent = $this->parent; - $model->name = $this->name; + $model->parent = $this->parent; + $model->name = $this->name; return $model; } diff --git a/src/model/concern/Attribute.php b/src/model/concern/Attribute.php index 6265ae80..409970c4 100644 --- a/src/model/concern/Attribute.php +++ b/src/model/concern/Attribute.php @@ -421,8 +421,8 @@ public function setAttr(string $name, $value, array $data = []): void $value = $this->writeTransform($value, $this->type[$name]); } elseif ($this->isRelationAttr($name)) { // 关联属性 - $this->relation[$name] = $value; - $this->with[$name] = true; + $this->relation[$name] = $value; + $this->with[$name] = true; } elseif ((array_key_exists($name, $this->origin) || empty($this->origin)) && $value instanceof Stringable) { // 对象类型 $value = $value->__toString(); @@ -458,16 +458,16 @@ protected function writeTransform($value, string|array $type) } return match ($type) { - 'integer' => (int) $value, - 'float' => empty($param) ? (float) $value : (float) number_format($value, (int) $param, '.', ''), - 'boolean' => (bool) $value, - 'timestamp' => !is_numeric($value) ? strtotime($value) : $value, - 'datetime' => $this->formatDateTime('Y-m-d H:i:s.u', $value, true), - 'object' => is_object($value) ? json_encode($value, JSON_FORCE_OBJECT) : $value, - 'array' => json_encode((array) $value, !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE), - 'json' => json_encode($value, !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE), - 'serialize' => serialize($value), - default => $value instanceof Stringable && str_contains($type, '\\') ? $value->__toString() : $value, + 'integer' => (int) $value, + 'float' => empty($param) ? (float) $value : (float) number_format($value, (int) $param, '.', ''), + 'boolean' => (bool) $value, + 'timestamp' => !is_numeric($value) ? strtotime($value) : $value, + 'datetime' => $this->formatDateTime('Y-m-d H:i:s.u', $value, true), + 'object' => is_object($value) ? json_encode($value, JSON_FORCE_OBJECT) : $value, + 'array' => json_encode((array) $value, !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE), + 'json' => json_encode($value, !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE), + 'serialize' => serialize($value), + default => $value instanceof Stringable && str_contains($type, '\\') ? $value->__toString() : $value, }; } @@ -483,11 +483,11 @@ protected function writeTransform($value, string|array $type) public function getAttr(string $name) { try { - $relation = false; - $value = $this->getData($name); + $relation = false; + $value = $this->getData($name); } catch (InvalidArgumentException $e) { - $relation = $this->isRelationAttr($name); - $value = null; + $relation = $this->isRelationAttr($name); + $value = null; } return $this->getValue($name, $value, $relation); @@ -618,16 +618,16 @@ protected function readTransform($value, string|array $type) }; return match ($type) { - 'integer' => (int) $value, - 'float' => empty($param) ? (float) $value : (float) number_format($value, (int) $param, '.', ''), - 'boolean' => (bool) $value, - 'timestamp' => !is_null($value) ? $this->formatDateTime(!empty($param) ? $param : $this->dateFormat, $value, true) : null, - 'datetime' => !is_null($value) ? $this->formatDateTime(!empty($param) ? $param : $this->dateFormat, $value) : null, - 'json' => json_decode($value, true), - 'array' => empty($value) ? [] : json_decode($value, true), - 'object' => empty($value) ? new \stdClass() : json_decode($value), - 'serialize' => $call($value), - default => str_contains($type, '\\') ? new $type($value) : $value, + 'integer' => (int) $value, + 'float' => empty($param) ? (float) $value : (float) number_format($value, (int) $param, '.', ''), + 'boolean' => (bool) $value, + 'timestamp' => !is_null($value) ? $this->formatDateTime(!empty($param) ? $param : $this->dateFormat, $value, true) : null, + 'datetime' => !is_null($value) ? $this->formatDateTime(!empty($param) ? $param : $this->dateFormat, $value) : null, + 'json' => json_decode($value, true), + 'array' => empty($value) ? [] : json_decode($value, true), + 'object' => empty($value) ? new \stdClass() : json_decode($value), + 'serialize' => $call($value), + default => str_contains($type, '\\') ? new $type($value) : $value, }; } diff --git a/src/model/concern/Conversion.php b/src/model/concern/Conversion.php index c972769e..35dd8d43 100644 --- a/src/model/concern/Conversion.php +++ b/src/model/concern/Conversion.php @@ -275,7 +275,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]); } @@ -298,16 +298,16 @@ protected function appendAttrToArray(array &$item, $key, array|string $name, arr { if (is_array($name)) { // 批量追加关联对象属性 - $relation = $this->getRelationWith($key, $hidden, $visible); + $relation = $this->getRelationWith($key, $hidden, $visible); $item[$key] = $relation ? $relation->append($name)->toArray() : []; } elseif (str_contains($name, '.')) { // 追加单个关联对象属性 [$key, $attr] = explode('.', $name); - $relation = $this->getRelationWith($key, $hidden, $visible); + $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); } @@ -315,7 +315,7 @@ protected function appendAttrToArray(array &$item, $key, array|string $name, arr 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 dcf1ab3b..85d113ad 100644 --- a/src/model/concern/OptimLock.php +++ b/src/model/concern/OptimLock.php @@ -66,8 +66,8 @@ protected function updateLockVersion(): void public function getWhere() { - $where = parent::getWhere(); - $optimLock = $this->getOptimLockField(); + $where = parent::getWhere(); + $optimLock = $this->getOptimLockField(); if ($optimLock && $lockVer = $this->getOrigin($optimLock)) { $where[] = [$optimLock, '=', $lockVer]; diff --git a/src/model/concern/RelationShip.php b/src/model/concern/RelationShip.php index 046f5982..8a48dc4f 100644 --- a/src/model/concern/RelationShip.php +++ b/src/model/concern/RelationShip.php @@ -139,8 +139,8 @@ public function setRelation(string $name, $value, array $data = []) $name = $this->getRealFieldName($name); - $this->relation[$name] = $value; - $this->with[$name] = true; + $this->relation[$name] = $value; + $this->with[$name] = true; return $this; } @@ -161,19 +161,19 @@ public function relationQuery(array $relations, array $withRelationAttr = []): v if ($relation instanceof Closure) { // 支持闭包查询过滤关联条件 - $closure = $relation; - $relation = $key; + $closure = $relation; + $relation = $key; } if (is_array($relation)) { - $subRelation = $relation; - $relation = $key; + $subRelation = $relation; + $relation = $key; } elseif (str_contains($relation, '.')) { [$relation, $subRelation] = explode('.', $relation, 2); } - $method = Str::camel($relation); - $relationName = Str::snake($relation); + $method = Str::camel($relation); + $relationName = Str::snake($relation); $relationResult = $this->$method(); if (isset($withRelationAttr[$relationName])) { @@ -251,8 +251,8 @@ 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(); + $relation = Str::camel($relation); + $class = $this->$relation(); if ($class instanceof OneToOne) { $class->eagerly($query, $relation, $field, $joinType, $closure, $first); @@ -277,25 +277,25 @@ public function eagerly(Query $query, string $relation, $field, string $joinType public function eagerlyResultSet(array &$resultSet, array $relations, array $withRelationAttr = [], bool $join = false, $cache = false): void { foreach ($relations as $key => $relation) { - $subRelation = []; - $closure = null; + $subRelation = []; + $closure = null; if ($relation instanceof Closure) { - $closure = $relation; - $relation = $key; + $closure = $relation; + $relation = $key; } if (is_array($relation)) { - $subRelation = $relation; - $relation = $key; + $subRelation = $relation; + $relation = $key; } elseif (str_contains($relation, '.')) { [$relation, $subRelation] = explode('.', $relation, 2); $subRelation = [$subRelation]; } - $relationName = $relation; - $relation = Str::camel($relation); + $relationName = $relation; + $relation = Str::camel($relation); $relationResult = $this->$relation(); if (isset($withRelationAttr[$relationName])) { @@ -327,25 +327,25 @@ public function eagerlyResultSet(array &$resultSet, array $relations, array $wit public function eagerlyResult(array $relations, array $withRelationAttr = [], bool $join = false, $cache = false): void { foreach ($relations as $key => $relation) { - $subRelation = []; - $closure = null; + $subRelation = []; + $closure = null; if ($relation instanceof Closure) { - $closure = $relation; - $relation = $key; + $closure = $relation; + $relation = $key; } if (is_array($relation)) { - $subRelation = $relation; - $relation = $key; + $subRelation = $relation; + $relation = $key; } elseif (str_contains($relation, '.')) { [$relation, $subRelation] = explode('.', $relation, 2); $subRelation = [$subRelation]; } - $relationName = $relation; - $relation = Str::camel($relation); + $relationName = $relation; + $relation = Str::camel($relation); $relationResult = $this->$relation(); if (isset($withRelationAttr[$relationName])) { @@ -377,8 +377,8 @@ public function bindAttr(string $relation, array $attrs = []) $relation = $this->getRelation($relation, true); foreach ($attrs as $key => $attr) { - $key = is_numeric($key) ? $attr : $key; - $value = $this->getOrigin($key); + $key = is_numeric($key) ? $attr : $key; + $value = $this->getOrigin($key); if (!is_null($value)) { throw new Exception('bind attr has exists:' . $key); @@ -407,11 +407,11 @@ public function relationCount(Query $query, array $relations, string $aggregate $closure = $name = null; if ($relation instanceof Closure) { - $closure = $relation; - $relation = $key; + $closure = $relation; + $relation = $key; } elseif (is_string($key)) { - $name = $relation; - $relation = $key; + $name = $relation; + $relation = $key; } $relation = Str::camel($relation); @@ -446,8 +446,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); @@ -465,11 +465,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); } @@ -486,8 +486,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); @@ -508,12 +508,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); } @@ -533,12 +533,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); } @@ -556,11 +556,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); } @@ -627,11 +627,11 @@ public function morphMany(string $model, string|array $morph = null, string $typ */ public function morphTo(string|array $morph = null, array $alias = []): MorphTo { - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); - $relation = Str::snake($trace[1]['function']); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); + $relation = Str::snake($trace[1]['function']); if (is_null($morph)) { - $morph = $relation; + $morph = $relation; } [$morphType, $foreignKey] = $this->parseMorph($morph); @@ -657,9 +657,9 @@ public function morphToMany(string $model, string $middle, string|array $morph = [$morphType, $morphKey] = $this->parseMorph($morph); - $model = $this->parseModel($model); - $name = Str::snake(class_basename($model)); - $localKey = $localKey ?: $this->getForeignKey($name); + $model = $this->parseModel($model); + $name = Str::snake(class_basename($model)); + $localKey = $localKey ?: $this->getForeignKey($name); return new MorphToMany($this, $model, $middle, $morphType, $morphKey, $localKey); } @@ -682,7 +682,7 @@ public function morphByMany(string $model, string $middle, string|array $morph = [$morphType, $morphKey] = $this->parseMorph($morph); - $model = $this->parseModel($model); + $model = $this->parseModel($model); $foreignKey = $foreignKey ?: $this->getForeignKey($this->name); return new MorphToMany($this, $model, $middle, $morphType, $morphKey, $foreignKey, true); @@ -700,7 +700,7 @@ protected function parseMorph(string|array $morph): array if (is_array($morph)) { [$morphType, $foreignKey] = $morph; } else { - $morphType = $morph . '_type'; + $morphType = $morph . '_type'; $foreignKey = $morph . '_id'; } @@ -855,7 +855,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/relation/BelongsTo.php b/src/model/relation/BelongsTo.php index 75771a3b..3c685277 100644 --- a/src/model/relation/BelongsTo.php +++ b/src/model/relation/BelongsTo.php @@ -33,12 +33,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->foreignKey = $foreignKey; - $this->localKey = $localKey; - $this->query = (new $model())->db(); - $this->relation = $relation; + $this->parent = $parent; + $this->model = $model; + $this->foreignKey = $foreignKey; + $this->localKey = $localKey; + $this->query = (new $model())->db(); + $this->relation = $relation; if (get_class($parent) == $model) { $this->selfRelation = true; @@ -59,8 +59,8 @@ public function getRelation(array $subRelation = [], Closure $closure = null) $closure($this->query); } - $foreignKey = $this->foreignKey; - $relationModel = $this->query + $foreignKey = $this->foreignKey; + $relationModel = $this->query ->removeWhereField($this->localKey) ->where($this->localKey, $this->parent->$foreignKey) ->relation($subRelation) @@ -144,13 +144,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]) @@ -174,9 +174,9 @@ 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); - $relation = class_basename($this->model); + $table = $this->query->getTable(); + $model = class_basename($this->parent); + $relation = class_basename($this->model); if (is_array($where)) { $this->getQueryWhere($where, $relation); @@ -187,9 +187,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) @@ -213,10 +213,10 @@ 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 = []; + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 if (isset($result->$foreignKey)) { @@ -268,7 +268,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 9c045b8c..6b2a866f 100644 --- a/src/model/relation/BelongsToMany.php +++ b/src/model/relation/BelongsToMany.php @@ -65,10 +65,10 @@ class BelongsToMany extends Relation */ public function __construct(Model $parent, string $model, string $middle, string $foreignKey, string $localKey) { - $this->parent = $parent; - $this->model = $model; - $this->foreignKey = $foreignKey; - $this->localKey = $localKey; + $this->parent = $parent; + $this->model = $model; + $this->foreignKey = $foreignKey; + $this->localKey = $localKey; if (str_contains($middle, '\\')) { $this->pivotName = $middle; @@ -242,9 +242,9 @@ 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 = []; + $localKey = $this->localKey; + $pk = $resultSet[0]->getPk(); + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 @@ -592,7 +592,7 @@ public function sync(array $ids, bool $detaching = true): array $changes = [ 'attached' => [], 'detached' => [], - 'updated' => [], + 'updated' => [], ]; $current = $this->pivot diff --git a/src/model/relation/HasMany.php b/src/model/relation/HasMany.php index 01854de5..71dbe0d9 100644 --- a/src/model/relation/HasMany.php +++ b/src/model/relation/HasMany.php @@ -34,11 +34,11 @@ class HasMany extends Relation */ public function __construct(Model $parent, string $model, string $foreignKey, string $localKey) { - $this->parent = $parent; - $this->model = $model; - $this->foreignKey = $foreignKey; - $this->localKey = $localKey; - $this->query = (new $model())->db(); + $this->parent = $parent; + $this->model = $model; + $this->foreignKey = $foreignKey; + $this->localKey = $localKey; + $this->query = (new $model())->db(); if (get_class($parent) == $model) { $this->selfRelation = true; @@ -79,8 +79,8 @@ 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 = []; + $localKey = $this->localKey; + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 @@ -122,8 +122,8 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio $localKey = $this->localKey; if (isset($result->$localKey)) { - $pk = $result->$localKey; - $data = $this->eagerlyOneToMany([ + $pk = $result->$localKey; + $data = $this->eagerlyOneToMany([ [$this->foreignKey, '=', $pk], ], $subRelation, $closure, $cache); @@ -300,9 +300,9 @@ public function saveAll(iterable $dataSet, bool $replace = true) */ public function has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = 'INNER', Query $query = null): Query { - $table = $this->query->getTable(); - $model = class_basename($this->parent); - $relation = class_basename($this->model); + $table = $this->query->getTable(); + $model = class_basename($this->parent); + $relation = class_basename($this->model); if ('*' != $id) { $id = $relation . '.' . (new $this->model())->getPk(); @@ -332,9 +332,9 @@ 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); - $relation = class_basename($this->model); + $table = $this->query->getTable(); + $model = class_basename($this->parent); + $relation = class_basename($this->model); if (is_array($where)) { $this->getQueryWhere($where, $relation); diff --git a/src/model/relation/HasManyThrough.php b/src/model/relation/HasManyThrough.php index f543117b..224fab0a 100644 --- a/src/model/relation/HasManyThrough.php +++ b/src/model/relation/HasManyThrough.php @@ -58,14 +58,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->foreignKey = $foreignKey; - $this->throughKey = $throughKey; - $this->localKey = $localKey; - $this->throughPk = $throughPk; - $this->query = (new $model())->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(); } /** @@ -102,13 +102,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(); - $relationTable = $relation->getTable(); - $softDelete = $this->query->getOptions('soft_delete'); + $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'); if ('*' != $id) { $id = $relationTable . '.' . $relation->getPk(); @@ -137,11 +137,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)); - $throughTable = $this->through->getTable(); - $pk = $this->throughPk; - $throughKey = $this->throughKey; - $modelTable = (new $this->model())->getTable(); + $model = Str::snake(class_basename($this->parent)); + $throughTable = $this->through->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = (new $this->model())->getTable(); if (is_array($where)) { $this->getQueryWhere($where, $modelTable); @@ -152,9 +152,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) @@ -180,10 +180,10 @@ 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 = []; + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 if (isset($result->$localKey)) { @@ -224,9 +224,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); @@ -256,8 +256,8 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio protected function eagerlyWhere(array $where, string $key, array $subRelation = [], Closure $closure = null, array $cache = []): array { // 预载入关联查询 支持嵌套预载入 - $throughList = $this->through->where($where)->select(); - $keys = $throughList->column($this->throughPk, $this->throughPk); + $throughList = $this->through->where($where)->select(); + $keys = $throughList->column($this->throughPk, $this->throughPk); if ($closure) { $this->baseQuery = true; @@ -354,11 +354,11 @@ public function getRelationCountQuery(Closure $closure = null, string $aggregate $closure($this->query, $name); } - $alias = Str::snake(class_basename($this->model)); - $throughTable = $this->through->getTable(); - $pk = $this->throughPk; - $throughKey = $this->throughKey; - $modelTable = $this->parent->getTable(); + $alias = Str::snake(class_basename($this->model)); + $throughTable = $this->through->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = $this->parent->getTable(); if (!str_contains($field, '.')) { $field = $alias . '.' . $field; @@ -381,12 +381,12 @@ 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)); - $throughTable = $this->through->getTable(); - $pk = $this->throughPk; - $throughKey = $this->throughKey; - $modelTable = $this->parent->getTable(); - $fields = $this->getQueryFields($alias); + $alias = Str::snake(class_basename($this->model)); + $throughTable = $this->through->getTable(); + $pk = $this->throughPk; + $throughKey = $this->throughKey; + $modelTable = $this->parent->getTable(); + $fields = $this->getQueryFields($alias); $this->query ->field($fields) diff --git a/src/model/relation/HasOne.php b/src/model/relation/HasOne.php index 437167ad..811fe980 100644 --- a/src/model/relation/HasOne.php +++ b/src/model/relation/HasOne.php @@ -32,11 +32,11 @@ class HasOne extends OneToOne */ public function __construct(Model $parent, string $model, string $foreignKey, string $localKey) { - $this->parent = $parent; - $this->model = $model; - $this->foreignKey = $foreignKey; - $this->localKey = $localKey; - $this->query = (new $model())->db(); + $this->parent = $parent; + $this->model = $model; + $this->foreignKey = $foreignKey; + $this->localKey = $localKey; + $this->query = (new $model())->db(); if (get_class($parent) == $model) { $this->selfRelation = true; @@ -144,13 +144,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]) @@ -174,9 +174,9 @@ 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); - $relation = class_basename($this->model); + $table = $this->query->getTable(); + $model = class_basename($this->parent); + $relation = class_basename($this->model); if (is_array($where)) { $this->getQueryWhere($where, $relation); @@ -187,9 +187,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) @@ -213,10 +213,10 @@ 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 = []; + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 if (isset($result->$localKey)) { @@ -268,7 +268,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 bc1986cf..8f88b907 100644 --- a/src/model/relation/HasOneThrough.php +++ b/src/model/relation/HasOneThrough.php @@ -61,10 +61,10 @@ 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 = []; + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 if (isset($result->$localKey)) { @@ -111,7 +111,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 e3ba6045..4c406105 100644 --- a/src/model/relation/MorphMany.php +++ b/src/model/relation/MorphMany.php @@ -56,12 +56,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->morphType = $morphType; - $this->query = (new $model())->db(); + $this->parent = $parent; + $this->model = $model; + $this->type = $type; + $this->morphKey = $morphKey; + $this->morphType = $morphType; + $this->query = (new $model())->db(); } /** @@ -129,10 +129,10 @@ 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 = []; + $morphType = $this->morphType; + $morphKey = $this->morphKey; + $type = $this->type; + $range = []; foreach ($resultSet as $result) { $pk = $result->getPk(); @@ -176,8 +176,8 @@ public function eagerlyResult(Model $result, string $relation, array $subRelatio $pk = $result->getPk(); if (isset($result->$pk)) { - $key = $result->$pk; - $data = $this->eagerlyMorphToMany([ + $key = $result->$pk; + $data = $this->eagerlyMorphToMany([ [$this->morphKey, '=', $key], [$this->morphType, '=', $this->type], ], $subRelation, $closure, $cache); @@ -322,7 +322,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 97bc954d..1cea5e2e 100644 --- a/src/model/relation/MorphOne.php +++ b/src/model/relation/MorphOne.php @@ -62,12 +62,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->morphType = $morphType; - $this->query = (new $model())->db(); + $this->parent = $parent; + $this->model = $model; + $this->type = $type; + $this->morphKey = $morphKey; + $this->morphType = $morphType; + $this->query = (new $model())->db(); } /** @@ -147,10 +147,10 @@ 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 = []; + $morphType = $this->morphType; + $morphKey = $this->morphKey; + $type = $this->type; + $range = []; foreach ($resultSet as $result) { $pk = $result->getPk(); @@ -297,7 +297,7 @@ public function make(array|Model $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/MorphTo.php b/src/model/relation/MorphTo.php index 6e1e837e..3d5651ca 100644 --- a/src/model/relation/MorphTo.php +++ b/src/model/relation/MorphTo.php @@ -65,11 +65,11 @@ class MorphTo extends Relation */ public function __construct(Model $parent, string $morphType, string $morphKey, array $alias = [], string $relation = null) { - $this->parent = $parent; - $this->morphType = $morphType; - $this->morphKey = $morphKey; - $this->alias = $alias; - $this->relation = $relation; + $this->parent = $parent; + $this->morphType = $morphType; + $this->morphKey = $morphKey; + $this->alias = $alias; + $this->relation = $relation; } /** @@ -95,8 +95,8 @@ public function getModel(): Model */ public function getRelation(array $subRelation = [], Closure $closure = null) { - $morphKey = $this->morphKey; - $morphType = $this->morphType; + $morphKey = $this->morphKey; + $morphType = $this->morphType; // 多态模型 $model = $this->parseModel($this->parent->$morphType); @@ -228,9 +228,9 @@ public function removeOption(string $option = '') */ public function eagerlyResultSet(array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []): void { - $morphKey = $this->morphKey; - $morphType = $this->morphType; - $range = []; + $morphKey = $this->morphKey; + $morphType = $this->morphType; + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 @@ -322,8 +322,8 @@ public function relationCount(Model $result, Closure $closure = null, string $ag protected function eagerlyMorphToOne(string $model, string $relation, Model $result, array $subRelation = [], array $cache = []): void { // 预载入关联查询 支持嵌套预载入 - $pk = $this->parent->{$this->morphKey}; - $data = null; + $pk = $this->parent->{$this->morphKey}; + $data = null; if (class_exists($model)) { $data = (new $model())->with($subRelation) @@ -349,9 +349,9 @@ protected function eagerlyMorphToOne(string $model, string $relation, Model $res */ public function associate(Model $model, string $type = ''): Model { - $morphKey = $this->morphKey; - $morphType = $this->morphType; - $pk = $model->getPk(); + $morphKey = $this->morphKey; + $morphType = $this->morphType; + $pk = $model->getPk(); $this->parent->setAttr($morphKey, $model->$pk); $this->parent->setAttr($morphType, $type ?: get_class($model)); @@ -367,8 +367,8 @@ public function associate(Model $model, string $type = ''): Model */ public function dissociate(): Model { - $morphKey = $this->morphKey; - $morphType = $this->morphType; + $morphKey = $this->morphKey; + $morphType = $this->morphType; $this->parent->setAttr($morphKey, null); $this->parent->setAttr($morphType, null); diff --git a/src/model/relation/MorphToMany.php b/src/model/relation/MorphToMany.php index 8fa59758..8606bf3d 100644 --- a/src/model/relation/MorphToMany.php +++ b/src/model/relation/MorphToMany.php @@ -65,16 +65,16 @@ 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->morphClass = $inverse ? $model : get_class($parent); + $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); } @@ -92,8 +92,8 @@ 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(); - $range = []; + $pk = $resultSet[0]->getPk(); + $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 @@ -215,9 +215,9 @@ public function getRelationCountQuery(Closure $closure = null, string $aggregate protected function belongsToManyQuery(string $foreignKey, string $localKey, array $condition = []): Query { // 关联查询封装 - $tableName = $this->query->getTable(); - $table = $this->pivot->db()->getTable(); - $fields = $this->getQueryFields($tableName); + $tableName = $this->query->getTable(); + $table = $this->pivot->db()->getTable(); + $fields = $this->getQueryFields($tableName); $query = $this->query ->field($fields) @@ -420,7 +420,7 @@ public function sync(array $ids, bool $detaching = true): array $changes = [ 'attached' => [], 'detached' => [], - 'updated' => [], + 'updated' => [], ]; $current = $this->pivot diff --git a/src/model/relation/OneToOne.php b/src/model/relation/OneToOne.php index c45489ed..7e6d8e19 100644 --- a/src/model/relation/OneToOne.php +++ b/src/model/relation/OneToOne.php @@ -90,9 +90,9 @@ public function eagerly(Query $query, string $relation, $field = true, string $j } // 预载入封装 - $joinTable = $this->query->getTable(); - $joinAlias = $relation; - $joinType = $joinType ?: $this->joinType; + $joinTable = $this->query->getTable(); + $joinAlias = $relation; + $joinType = $joinType ?: $this->joinType; $query->via($joinAlias); @@ -315,8 +315,8 @@ 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; - $value = $result->getOrigin($key); + $key = is_numeric($key) ? $attr : $key; + $value = $result->getOrigin($key); if (!is_null($value)) { throw new Exception('bind attr has exists:' . $key); diff --git a/src/paginator/driver/Bootstrap.php b/src/paginator/driver/Bootstrap.php index dec1275a..5c778276 100644 --- a/src/paginator/driver/Bootstrap.php +++ b/src/paginator/driver/Bootstrap.php @@ -69,9 +69,9 @@ protected function getLinks(): string } $block = [ - 'first' => null, + 'first' => null, 'slider' => null, - 'last' => null, + 'last' => null, ]; $side = 3; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 30fd0525..72b48fcf 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,12 +8,12 @@ // 数据库配置信息设置(全局有效) Db::setConfig([ // 默认数据连接标识 - 'default' => 'mysql', + 'default' => 'mysql', // 数据库连接信息 'connections' => [ 'mysql' => [ // 数据库类型 - 'type' => 'mysql', + 'type' => 'mysql', // 主机地址 'hostname' => getenv('TESTS_DB_MYSQL_HOSTNAME'), // 数据库名 @@ -23,19 +23,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'), // 数据库名 @@ -45,11 +45,11 @@ // 密码 'password' => getenv('TESTS_DB_MYSQL_PASSWORD'), // 数据库编码默认采用utf8 - 'charset' => 'utf8', + 'charset' => 'utf8', // 数据库表前缀 - 'prefix' => 'test_', + 'prefix' => 'test_', // 数据库调试模式 - 'debug' => false, + 'debug' => false, ], ], ]);