From 3b84e09f001ab724f0c0f6aa7a8383eab8e7d3ab Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 4 Feb 2023 18:16:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/BaseQuery.php | 71 +++++++++++++------------- src/db/builder/Mysql.php | 11 ++-- src/db/builder/Oracle.php | 8 +-- src/db/builder/Pgsql.php | 4 +- src/db/builder/Sqlite.php | 6 +-- src/db/builder/Sqlsrv.php | 4 +- src/db/connector/Mongo.php | 81 +++++++++++++----------------- src/db/connector/Mysql.php | 12 ++--- src/db/connector/Oracle.php | 12 ++--- src/db/connector/Pgsql.php | 12 ++--- src/db/connector/Sqlite.php | 10 ++-- src/db/connector/Sqlsrv.php | 28 +++++------ src/model/concern/Attribute.php | 12 ++--- src/model/concern/Conversion.php | 10 ++-- src/model/concern/OptimLock.php | 4 +- src/model/concern/RelationShip.php | 74 +++++++++++++-------------- 16 files changed, 172 insertions(+), 187 deletions(-) diff --git a/src/db/BaseQuery.php b/src/db/BaseQuery.php index 1cc94249..d20d9d8e 100644 --- a/src/db/BaseQuery.php +++ b/src/db/BaseQuery.php @@ -82,9 +82,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'); } /** @@ -378,7 +377,7 @@ public function field(string|array|Raw|bool $field) if (true === $field) { // 获取全部字段 $fields = $this->getTableFields(); - $field = $fields ?: ['*']; + $field = $fields ?: ['*']; } if (isset($this->options['field'])) { @@ -409,7 +408,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); @@ -443,7 +442,7 @@ public function tableField(string|array|bool $field, string $tableName, string $ if (true === $field) { // 获取全部字段 $fields = $this->getTableFields($tableName); - $field = $fields ?: ['*']; + $field = $fields ?: ['*']; } // 添加统一的前缀 @@ -490,8 +489,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]); } @@ -670,7 +669,7 @@ 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; } @@ -682,17 +681,15 @@ public function paginate(int|array $listRows = null, int|bool $simple = false): ]; 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 = $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(); if (!isset($total) && !$simple) { @@ -700,8 +697,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 { @@ -712,10 +709,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'); @@ -744,15 +741,15 @@ public function paginateX(int|array $listRows = null, string $key = null, string '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 = $page < 1 ? 1 : $page; + $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; $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'] ?? ''; @@ -856,11 +853,11 @@ public function cache($key = true, $expire = null, $tag = null, bool $always = f 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->options['cache_always'] = $always; + $this->options['cache'] = [$key, $expire, $tag]; + $this->options['cache_always'] = $always; return $this; } @@ -969,8 +966,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; } @@ -1329,9 +1326,11 @@ 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); + + $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/Mysql.php b/src/db/builder/Mysql.php index 72e316ad..7e15f29f 100644 --- a/src/db/builder/Mysql.php +++ b/src/db/builder/Mysql.php @@ -161,10 +161,8 @@ public function insert(Query $query): string */ public function insertAll(Query $query, array $dataSet, bool $replace = false): string { - $options = $query->getOptions(); - - // 获取绑定信息 - $bind = $query->getFieldsBindType(); + $options = $query->getOptions(); + $bind = $query->getFieldsBindType(); // 获取合法的字段 if (empty($options['field']) || '*' == $options['field']) { @@ -215,9 +213,8 @@ public function insertAll(Query $query, array $dataSet, bool $replace = false): */ 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/builder/Oracle.php b/src/db/builder/Oracle.php index 82648433..d353b87d 100644 --- a/src/db/builder/Oracle.php +++ b/src/db/builder/Oracle.php @@ -52,11 +52,11 @@ protected function parseLimit(Query $query, string $limit): string * 设置锁机制. * * @param Query $query 查询对象 - * @param bool|false $lock + * @param bool|string $lock * * @return string */ - protected function parseLock(Query $query, $lock = false): string + protected function parseLock(Query $query, bool|string $lock = false): string { if (!$lock) { return ''; @@ -69,14 +69,14 @@ protected function parseLock(Query $query, $lock = false): string * 字段和表名处理. * * @param Query $query 查询对象 - * @param string $key + * @param string|int|Raw $key * @param bool $strict * * @throws Exception * * @return string */ - public function parseKey(Query $query, $key, bool $strict = false): string + public function parseKey(Query $query, string|int|Raw $key, bool $strict = false): string { if (is_int($key)) { return (string) $key; diff --git a/src/db/builder/Pgsql.php b/src/db/builder/Pgsql.php index 93fa800c..011cc8be 100644 --- a/src/db/builder/Pgsql.php +++ b/src/db/builder/Pgsql.php @@ -64,12 +64,12 @@ public function parseLimit(Query $query, string $limit): string * 字段和表名处理. * * @param Query $query 查询对象 - * @param mixed $key 字段名 + * @param string|int|Raw $key 字段名 * @param bool $strict 严格检测 * * @return string */ - public function parseKey(Query $query, $key, bool $strict = false): string + public function parseKey(Query $query, string|int|Raw $key, bool $strict = false): string { if (is_int($key)) { return (string) $key; diff --git a/src/db/builder/Sqlite.php b/src/db/builder/Sqlite.php index 71feb2b2..b2f2c392 100644 --- a/src/db/builder/Sqlite.php +++ b/src/db/builder/Sqlite.php @@ -62,12 +62,12 @@ protected function parseRand(Query $query): string * 字段和表名处理. * * @param Query $query 查询对象 - * @param mixed $key 字段名 + * @param string|int|Raw $key 字段名 * @param bool $strict 严格检测 * * @return string */ - public function parseKey(Query $query, $key, bool $strict = false): string + public function parseKey(Query $query, string|int|Raw $key, bool $strict = false): string { if (is_int($key)) { return (string) $key; @@ -111,7 +111,7 @@ public function parseKey(Query $query, $key, bool $strict = false): string * * @return string */ - protected function parseLock(Query $query, $lock = false): string + protected function parseLock(Query $query, bool|string $lock = false): string { return ''; } diff --git a/src/db/builder/Sqlsrv.php b/src/db/builder/Sqlsrv.php index 13a6beca..29201fb1 100644 --- a/src/db/builder/Sqlsrv.php +++ b/src/db/builder/Sqlsrv.php @@ -115,12 +115,12 @@ protected function parseRand(Query $query): string * 字段和表名处理. * * @param Query $query 查询对象 - * @param mixed $key 字段名 + * @param string|int|Raw $key 字段名 * @param bool $strict 严格检测 * * @return string */ - public function parseKey(Query $query, $key, bool $strict = false): string + public function parseKey(Query $query, string|int|Raw $key, bool $strict = false): string { if (is_int($key)) { return (string) $key; diff --git a/src/db/connector/Mongo.php b/src/db/connector/Mongo.php index 0a5993f5..dc527c5a 100644 --- a/src/db/connector/Mongo.php +++ b/src/db/connector/Mongo.php @@ -218,8 +218,7 @@ public function cursor($query) // 生成MongoQuery对象 $mongoQuery = $this->builder->select($query); - - $master = $query->getOptions('master') ? true : false; + $master = $query->getOptions('master') ? true : false; // 执行查询操作 return $this->getCursor($query, $mongoQuery, $master); @@ -244,8 +243,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; @@ -334,8 +333,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); @@ -379,9 +378,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; } @@ -412,9 +410,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); @@ -594,11 +592,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,10 +683,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']; @@ -745,18 +742,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); @@ -820,9 +816,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(); } @@ -846,11 +841,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); @@ -879,12 +872,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); @@ -990,7 +981,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; @@ -1035,8 +1026,8 @@ public function column(BaseQuery $query, $field, string $key = ''): array 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 fe7396a0..f18da995 100644 --- a/src/db/connector/Mysql.php +++ b/src/db/connector/Mysql.php @@ -64,10 +64,10 @@ 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) { @@ -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 0839fd9c..3877b973 100644 --- a/src/db/connector/Oracle.php +++ b/src/db/connector/Oracle.php @@ -54,11 +54,11 @@ 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 (+)"; - $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) { @@ -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 b2eeada2..3779c965 100644 --- a/src/db/connector/Pgsql.php +++ b/src/db/connector/Pgsql.php @@ -59,11 +59,11 @@ 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 . '\');'; - $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) { @@ -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 c03bfac7..ae201f00 100644 --- a/src/db/connector/Sqlite.php +++ b/src/db/connector/Sqlite.php @@ -44,11 +44,11 @@ protected function parseDsn(array $config): string 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) { @@ -81,9 +81,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 a499d050..b925891a 100644 --- a/src/db/connector/Sqlsrv.php +++ b/src/db/connector/Sqlsrv.php @@ -65,17 +65,17 @@ 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 - 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); + + $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); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); - $info = []; + $info = []; if (!empty($result)) { foreach ($result as $key => $val) { @@ -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/model/concern/Attribute.php b/src/model/concern/Attribute.php index 0dcf29a1..8b0ddffb 100644 --- a/src/model/concern/Attribute.php +++ b/src/model/concern/Attribute.php @@ -501,11 +501,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); @@ -643,13 +643,13 @@ protected function readTransform($value, string|array $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 6afb757e..e6e8d5df 100644 --- a/src/model/concern/Conversion.php +++ b/src/model/concern/Conversion.php @@ -272,7 +272,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]); } @@ -300,18 +300,18 @@ protected function appendAttrToArray(array &$item, $key, array|string $name) { if (is_array($name)) { // 追加关联对象属性 - $relation = $this->getRelation($key, true); + $relation = $this->getRelation($key, true); $item[$key] = $relation ? $relation->append($name) ->toArray() : []; } elseif (str_contains($name, '.')) { [$key, $attr] = explode('.', $name); // 追加关联对象属性 - $relation = $this->getRelation($key, true); + $relation = $this->getRelation($key, true); $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); } diff --git a/src/model/concern/OptimLock.php b/src/model/concern/OptimLock.php index 85d113ad..dcf1ab3b 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 0f10559c..eb346d1d 100644 --- a/src/model/concern/RelationShip.php +++ b/src/model/concern/RelationShip.php @@ -151,19 +151,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])) { @@ -241,8 +241,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); @@ -267,26 +267,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])) { @@ -316,26 +315,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])) { @@ -367,8 +365,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); @@ -397,11 +395,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); @@ -436,8 +434,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); @@ -617,11 +615,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); @@ -843,7 +841,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) {