From 464f1cc24655382691ef8db1403e1bb983dbfbc7 Mon Sep 17 00:00:00 2001 From: XiangMin Liu <635750556@qq.com> Date: Fri, 17 Feb 2023 23:13:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Infra/Model.php | 10 ++++---- app/Infra/ModelTest.php | 25 ++++++++++--------- .../migrations/20230217145516_base_brand.php | 6 ++--- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/app/Infra/Model.php b/app/Infra/Model.php index 120fd6a6..ec29dfbb 100644 --- a/app/Infra/Model.php +++ b/app/Infra/Model.php @@ -326,7 +326,7 @@ public function sum(string $field = '*'): string|int|float /** * 获取一条记录的某个字段值. */ - public function getField(string $field, null|string|bool|int $separator = null): string|array|int|float + public function getField(string $field, null|string|bool|int $separator = null): string|array|int|float|null { $options['field'] = $field; $options = $this->_parseOptions($options); @@ -418,17 +418,17 @@ public function getDbFields(): array|false return false; } - public function min(string $field = '*'): string|int|float + public function min(string $field = '*'): string|int|float|null { return $this->callStatisticalQuery('min', $field); } - public function max(string $field = '*'): string|int|float + public function max(string $field = '*'): string|int|float|null { return $this->callStatisticalQuery('max', $field); } - public function avg(string $field = '*'): string|int|float + public function avg(string $field = '*'): string|int|float|null { return $this->callStatisticalQuery('avg', $field); } @@ -1415,7 +1415,7 @@ protected function _checkTableInfo(): void } } - protected function callStatisticalQuery(string $method, string $field): string|int|float + protected function callStatisticalQuery(string $method, string $field): string|int|float|null { try { $this->shouldCountSelect = true; diff --git a/app/Infra/ModelTest.php b/app/Infra/ModelTest.php index 8626d908..477ad27f 100644 --- a/app/Infra/ModelTest.php +++ b/app/Infra/ModelTest.php @@ -645,7 +645,7 @@ public function testQuerySub29(): void $result = trim($baseBrandModel->getLastSql()); $sql = 'SELECT MAX(brand_id) AS max FROM `base_brand` LIMIT 1'; static::assertSame($result, $sql); - static::assertIsInt($count); + static::assertTrue(null === $count || \is_int($count)); } public function testQuerySub30(): void @@ -657,7 +657,7 @@ public function testQuerySub30(): void $result = trim($baseBrandModel->getLastSql()); $sql = 'SELECT MIN(brand_id) AS min FROM `base_brand` WHERE ( brand_id>0 ) LIMIT 1'; static::assertSame($result, $sql); - static::assertIsInt($count); + static::assertTrue(null === $count || \is_int($count)); } public function testQuerySub31(): void @@ -1282,11 +1282,12 @@ public function testQuerySub74(): void $baseBrandModel = BaseBrandModel::make(); $baseBrandModel ->field('SUM(brand_id),`brand_name`,`brand_logo` as logo') + ->group('brand_id') ->select() ; $result = $baseBrandModel->getLastSql(); $result = trim($baseBrandModel->getLastSql()); - $sql = 'SELECT SUM(brand_id),`brand_name`,`brand_logo` as logo FROM `base_brand`'; + $sql = 'SELECT SUM(brand_id),`brand_name`,`brand_logo` as logo FROM `base_brand` GROUP BY brand_id'; static::assertSame($result, $sql); } @@ -1626,13 +1627,13 @@ public function testQuerySub97(): void $baseBrandModel = BaseBrandModel::make(); $baseBrandModel ->where('status>1') - ->field('brand_name,max(brand_id)') - ->group('brand_logo') + ->field('brand_logo,brand_name,max(brand_id)') + ->group('brand_logo,brand_name') ->select() ; $result = $baseBrandModel->getLastSql(); $result = trim($baseBrandModel->getLastSql()); - $sql = 'SELECT `brand_name`,max(brand_id) FROM `base_brand` WHERE ( status>1 ) GROUP BY brand_logo'; + $sql = 'SELECT `brand_logo`,`brand_name`,max(brand_id) FROM `base_brand` WHERE ( status>1 ) GROUP BY brand_logo,brand_name'; static::assertSame($result, $sql); } @@ -1641,13 +1642,13 @@ public function testQuerySub98(): void $baseBrandModel = BaseBrandModel::make(); $baseBrandModel ->where('status>1') - ->field('`brand_name`,max(brand_id)') - ->group('`brand_logo`,status') + ->field('brand_logo,status,max(brand_id)') + ->group('brand_logo,status') ->select() ; $result = $baseBrandModel->getLastSql(); $result = trim($baseBrandModel->getLastSql()); - $sql = 'SELECT `brand_name`,max(brand_id) FROM `base_brand` WHERE ( status>1 ) GROUP BY `brand_logo`,status'; + $sql = 'SELECT `brand_logo`,`status`,max(brand_id) FROM `base_brand` WHERE ( status>1 ) GROUP BY brand_logo,status'; static::assertSame($result, $sql); } @@ -1656,14 +1657,14 @@ public function testQuerySub99(): void $baseBrandModel = BaseBrandModel::make(); $baseBrandModel ->where('status>1') - ->field('`brand_name`,max(brand_id)') - ->group('`brand_logo`,status') + ->field('brand_logo,status,max(brand_id)') + ->group('brand_logo,status') ->having('count(status)>3') ->select() ; $result = $baseBrandModel->getLastSql(); $result = trim($baseBrandModel->getLastSql()); - $sql = 'SELECT `brand_name`,max(brand_id) FROM `base_brand` WHERE ( status>1 ) GROUP BY `brand_logo`,status HAVING count(status)>3'; + $sql = 'SELECT `brand_logo`,`status`,max(brand_id) FROM `base_brand` WHERE ( status>1 ) GROUP BY brand_logo,status HAVING count(status)>3'; static::assertSame($result, $sql); } diff --git a/assets/database/migrations/20230217145516_base_brand.php b/assets/database/migrations/20230217145516_base_brand.php index 477a0ce6..a800f0a8 100644 --- a/assets/database/migrations/20230217145516_base_brand.php +++ b/assets/database/migrations/20230217145516_base_brand.php @@ -22,17 +22,17 @@ private function struct(): void $sql = <<<'EOT' CREATE TABLE `base_brand` ( `brand_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', - `company_id` bigint(20) unsigned NOT NULL COMMENT '公司ID', + `company_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '公司ID', `status` enum('T','F') NOT NULL DEFAULT 'T' COMMENT '状态', `order_num` bigint(20) unsigned NOT NULL DEFAULT '500' COMMENT '排序', `brand_num` varchar(30) NOT NULL DEFAULT '' COMMENT '编号', `brand_name` varchar(30) NOT NULL DEFAULT '' COMMENT '名称', `brand_logo` varchar(130) NOT NULL DEFAULT '' COMMENT 'LOGO', - `brand_about` text NOT NULL COMMENT '介绍', + `brand_about` text COMMENT '介绍', `update_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `create_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `brand_letter` varchar(30) NOT NULL DEFAULT '' COMMENT '品牌首字母', - `seo_keywords` varchar(30) NOT NULL COMMENT 'SEO关键字', + `seo_keywords` varchar(30) NOT NULL DEFAULT '' COMMENT 'SEO关键字', PRIMARY KEY (`brand_id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; EOT;