From c109c0f670aeece73a82222f8448f433ac89b5d3 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 11 Jun 2025 21:10:10 +0700 Subject: [PATCH 1/5] Update doc about `$db->select()` --- README.md | 3 +- docs/guide/en/query-builder.md | 3 +- docs/guide/en/query/select.md | 4 +-- docs/guide/en/query/union.md | 4 +-- docs/guide/en/query/where.md | 2 +- docs/guide/en/query/with-query.md | 9 ++---- docs/guide/en/schema/typecasting.md | 4 +-- docs/guide/pt-BR/query-builder.md | 3 +- docs/guide/pt-BR/query/select.md | 4 +-- docs/guide/pt-BR/query/union.md | 4 +-- docs/guide/pt-BR/query/where.md | 2 +- docs/guide/pt-BR/query/with-query.md | 9 ++---- src/Query/QueryInterface.php | 3 +- tests/AbstractConnectionTest.php | 2 +- tests/Common/CommonCommandTest.php | 32 ++++++++----------- .../Expression/ArrayExpressionBuilderTest.php | 2 +- .../Expression/JsonExpressionBuilderTest.php | 2 +- .../StructuredExpressionBuilderTest.php | 2 +- tests/Db/Query/QueryTest.php | 22 ++++++------- 19 files changed, 50 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 37ed32e2b..e17184817 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,7 @@ Using the package, you can perform common database tasks such as creating, readi records in a database table, as well as executing raw SQL queries. ```php -$rows = (new Query($db)) - ->select(['id', 'email']) +$rows = $db->select(['id', 'email']) ->from('{{%user}}') ->where(['last_name' => 'Smith']) ->limit(10) diff --git a/docs/guide/en/query-builder.md b/docs/guide/en/query-builder.md index 94261849e..c787e6c38 100644 --- a/docs/guide/en/query-builder.md +++ b/docs/guide/en/query-builder.md @@ -20,8 +20,7 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$rows = (new Query($db)) - ->select(['id', 'email']) +$rows = $db->select(['id', 'email']) ->from('{{%user}}') ->where(['last_name' => 'Smith']) ->limit(10) diff --git a/docs/guide/en/query/select.md b/docs/guide/en/query/select.md index 7c4e64d8a..d942a9711 100644 --- a/docs/guide/en/query/select.md +++ b/docs/guide/en/query/select.md @@ -69,8 +69,8 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$subQuery = (new Query($db))->select('COUNT(*)')->from('{{%user}}'); -$query = (new Query($db))->select(['id', 'count' => $subQuery])->from('{{%post}}'); +$subQuery = $db->select('COUNT(*)')->from('{{%user}}'); +$query = $db->select(['id', 'count' => $subQuery])->from('{{%post}}'); ``` The equivalent SQL is: diff --git a/docs/guide/en/query/union.md b/docs/guide/en/query/union.md index 5374b356f..e1a0a4fd6 100644 --- a/docs/guide/en/query/union.md +++ b/docs/guide/en/query/union.md @@ -10,8 +10,8 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$query1 = (new Query($db))->select("id, category_id AS type, name")->from('{{%post}}')->limit(10); -$query2 = (new Query($db))->select('id, type, name')->from('{{%user}}')->limit(10); +$query1 = $db->select("id, category_id AS type, name")->from('{{%post}}')->limit(10); +$query2 = $db->select('id, type, name')->from('{{%user}}')->limit(10); $query1->union($query2); ``` diff --git a/docs/guide/en/query/where.md b/docs/guide/en/query/where.md index ddbc185ea..00408036d 100644 --- a/docs/guide/en/query/where.md +++ b/docs/guide/en/query/where.md @@ -68,7 +68,7 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$userQuery = (new Query($db))->select('id')->from('user'); +$userQuery = $db->select('id')->from('user'); $query->where(['id' => $userQuery]); ``` diff --git a/docs/guide/en/query/with-query.md b/docs/guide/en/query/with-query.md index b1aec3c1c..79c15a2cb 100644 --- a/docs/guide/en/query/with-query.md +++ b/docs/guide/en/query/with-query.md @@ -12,18 +12,15 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$initialQuery = (new Query($db)) - ->select(['parent', 'child']) +$initialQuery = $db->select(['parent', 'child']) ->from(['aic' => '{{%auth_item_child}}']) ->where(['parent' => 'admin']); -$recursiveQuery = (new Query($db)) - ->select(['aic.parent', 'aic.child']) +$recursiveQuery = $db->select(['aic.parent', 'aic.child']) ->from(['aic' => '{{%auth_item_child}}']) ->innerJoin('t1', 't1.child = aic.parent'); -$mainQuery = (new Query($db)) - ->select(['parent', 'child']) +$mainQuery = $db->select(['parent', 'child']) ->from('{{%t1}}') ->withQuery($initialQuery->union($recursiveQuery), 't1', true); ``` diff --git a/docs/guide/en/schema/typecasting.md b/docs/guide/en/schema/typecasting.md index 79f5099ae..a9bddd052 100644 --- a/docs/guide/en/schema/typecasting.md +++ b/docs/guide/en/schema/typecasting.md @@ -105,7 +105,7 @@ the values when retrieving them from the database. Using these methods, values a they are returned. ```php -$query = (new Query($db))->from('customer')->where(['id' => 1]); +$query = $db->select()->from('customer')->where(['id' => 1]); $row = $query->withTypecasting()->one(); $isActive = $row['is_active']; @@ -126,7 +126,7 @@ Some databases support date and time types, such as `timestamp`, `datetime`, `da These types are casted to `DateTimeImmutable` objects using `DateTimeColumn` class when type casting is enabled. ```php -$query = (new Query($db))->from('customer')->where(['id' => 1]); +$query = $db->select()->from('customer')->where(['id' => 1]); $row = $query->withTypecasting()->one(); $createdAt = $row['created_at']; // `DateTimeImmutable` object diff --git a/docs/guide/pt-BR/query-builder.md b/docs/guide/pt-BR/query-builder.md index 52f9005cc..24c11d8b5 100644 --- a/docs/guide/pt-BR/query-builder.md +++ b/docs/guide/pt-BR/query-builder.md @@ -20,8 +20,7 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$rows = (new Query($db)) - ->select(['id', 'email']) +$rows = $db->select(['id', 'email']) ->from('{{%user}}') ->where(['last_name' => 'Smith']) ->limit(10) diff --git a/docs/guide/pt-BR/query/select.md b/docs/guide/pt-BR/query/select.md index bfe6cba2a..5aea31587 100644 --- a/docs/guide/pt-BR/query/select.md +++ b/docs/guide/pt-BR/query/select.md @@ -69,8 +69,8 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$subQuery = (new Query($db))->select('COUNT(*)')->from('{{%user}}'); -$query = (new Query($db))->select(['id', 'count' => $subQuery])->from('{{%post}}'); +$subQuery = $db->select('COUNT(*)')->from('{{%user}}'); +$query = $db->select(['id', 'count' => $subQuery])->from('{{%post}}'); ``` O SQL equivalente é: diff --git a/docs/guide/pt-BR/query/union.md b/docs/guide/pt-BR/query/union.md index c4bd36bf6..c72566638 100644 --- a/docs/guide/pt-BR/query/union.md +++ b/docs/guide/pt-BR/query/union.md @@ -10,8 +10,8 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$query1 = (new Query($db))->select("id, category_id AS type, name")->from('{{%post}}')->limit(10); -$query2 = (new Query($db))->select('id, type, name')->from('{{%user}}')->limit(10); +$query1 = $db->select("id, category_id AS type, name")->from('{{%post}}')->limit(10); +$query2 = $db->select('id, type, name')->from('{{%user}}')->limit(10); $query1->union($query2); ``` diff --git a/docs/guide/pt-BR/query/where.md b/docs/guide/pt-BR/query/where.md index ca246d07f..4c95ebb4a 100644 --- a/docs/guide/pt-BR/query/where.md +++ b/docs/guide/pt-BR/query/where.md @@ -68,7 +68,7 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$userQuery = (new Query($db))->select('id')->from('user'); +$userQuery = $db->select('id')->from('user'); $query->where(['id' => $userQuery]); ``` diff --git a/docs/guide/pt-BR/query/with-query.md b/docs/guide/pt-BR/query/with-query.md index 5d34929cf..48fe02780 100644 --- a/docs/guide/pt-BR/query/with-query.md +++ b/docs/guide/pt-BR/query/with-query.md @@ -12,18 +12,15 @@ use Yiisoft\Db\Query\Query; /** @var ConnectionInterface $db */ -$initialQuery = (new Query($db)) - ->select(['parent', 'child']) +$initialQuery = $db->select(['parent', 'child']) ->from(['aic' => '{{%auth_item_child}}']) ->where(['parent' => 'admin']); -$recursiveQuery = (new Query($db)) - ->select(['aic.parent', 'aic.child']) +$recursiveQuery = $db->select(['aic.parent', 'aic.child']) ->from(['aic' => '{{%auth_item_child}}']) ->innerJoin('t1', 't1.child = aic.parent'); -$mainQuery = (new Query($db)) - ->select(['parent', 'child']) +$mainQuery = $db->select(['parent', 'child']) ->from('{{%t1}}') ->withQuery($initialQuery->union($recursiveQuery), 't1', true); ``` diff --git a/src/Query/QueryInterface.php b/src/Query/QueryInterface.php index 7a6436daf..9ddad9ba9 100644 --- a/src/Query/QueryInterface.php +++ b/src/Query/QueryInterface.php @@ -306,8 +306,7 @@ public function prepare(QueryBuilderInterface $builder): self; * For example: * * ```php - * $users = (new Query($db)) - * ->from('user') + * $users = $db->select()->from('user') * ->resultCallback(function (array $rows): array { * foreach ($rows as &$row) { * $row['name'] = strtoupper($row['name']); diff --git a/tests/AbstractConnectionTest.php b/tests/AbstractConnectionTest.php index 0c2e70d2f..469a42355 100644 --- a/tests/AbstractConnectionTest.php +++ b/tests/AbstractConnectionTest.php @@ -37,7 +37,7 @@ public function testCreateBatchQueryResult(): void { $db = $this->getConnection(); - $query = (new Query($db))->from('customer'); + $query = $db->select()->from('customer'); $this->assertInstanceOf(BatchQueryResult::class, $db->createBatchQueryResult($query)); } diff --git a/tests/Common/CommonCommandTest.php b/tests/Common/CommonCommandTest.php index 071bc64b1..9eefef7bb 100644 --- a/tests/Common/CommonCommandTest.php +++ b/tests/Common/CommonCommandTest.php @@ -319,7 +319,7 @@ public function testBatchInsert( $command->prepare(false); $command->execute(); - $this->assertEquals($insertedRow, (new Query($db))->from($table)->count()); + $this->assertEquals($insertedRow, $db->select()->from($table)->count()); $db->close(); } @@ -418,8 +418,7 @@ public function testBatchInsertWithDuplicates(): void $this->assertSame(1, $command->execute()); - $result = (new Query($db)) - ->select(['email', 'name', 'address']) + $result = $db->select(['email', 'name', 'address']) ->from('{{customer}}') ->where(['=', '{{email}}', 't1@example.com']) ->one(); @@ -451,7 +450,7 @@ public function testBatchInsertWithManyData(): void $this->assertSame($attemptsInsertRows, $command->execute()); - $insertedRowsCount = (new Query($db))->from('{{customer}}')->count(); + $insertedRowsCount = $db->select()->from('{{customer}}')->count(); $this->assertGreaterThanOrEqual($attemptsInsertRows, $insertedRowsCount); @@ -565,7 +564,7 @@ public function testCreateView(): void $command = $db->createCommand(); $schema = $db->getSchema(); - $subQuery = (new Query($db))->select('{{bar}}')->from('{{testCreateViewTable}}')->where(['>', 'bar', '5']); + $subQuery = $db->select('{{bar}}')->from('{{testCreateViewTable}}')->where(['>', 'bar', '5']); if ($schema->getTableSchema('{{testCreateView}}') !== null) { $command->dropView('{{testCreateView}}')->execute(); @@ -1318,7 +1317,7 @@ public function testsInsertQueryAsColumnValue(): void $orderId = $db->getLastInsertId(); } - $columnValueQuery = (new Query($db))->select('{{created_at}}')->from('{{order}}')->where(['id' => $orderId]); + $columnValueQuery = $db->select('{{created_at}}')->from('{{order}}')->where(['id' => $orderId]); $command->insert( '{{order_with_null_fk}}', ['customer_id' => $orderId, 'created_at' => $columnValueQuery, 'total' => 42], @@ -1366,8 +1365,7 @@ public function testInsertSelect(): void '{{customer}}', ['email' => 't1@example.com', 'name' => 'test', 'address' => 'test address'] )->execute(); - $query = (new Query($db)) - ->select(['{{customer}}.{{email}} as name', '{{name}} as email', '{{address}}']) + $query = $db->select(['{{customer}}.{{email}} as name', '{{name}} as email', '{{address}}']) ->from('{{customer}}') ->where(['and', ['<>', 'name', 'foo'], ['status' => [0, 1, 2, 3]]]); $command->insert('{{customer}}', $query)->execute(); @@ -1417,8 +1415,7 @@ public function testInsertSelectAlias(): void 'address' => 'test address', ] )->execute(); - $query = (new Query($db)) - ->select(['email' => '{{customer}}.{{email}}', 'address' => 'name', 'name' => 'address']) + $query = $db->select(['email' => '{{customer}}.{{email}}', 'address' => 'name', 'name' => 'address']) ->from('{{customer}}') ->where(['and', ['<>', 'name', 'foo'], ['status' => [0, 1, 2, 3]]]); $command->insert('{{customer}}', $query)->execute(); @@ -2029,7 +2026,7 @@ public function testUpdate( $this->assertSame($expectedCount, $count); - $values = (new Query($db)) + $values = $db->select() ->from($table) ->where($conditions, $params) ->limit(1) @@ -2161,8 +2158,7 @@ protected function performAndCompareUpsertResult(PdoConnectionInterface $db, arr $command->execute(); - $actual = (new Query($db)) - ->select(['email', 'address' => new Expression($this->upsertTestCharCast), 'status']) + $actual = $db->select(['email', 'address' => new Expression($this->upsertTestCharCast), 'status']) ->from('{{T_upsert}}') ->one(); $this->assertEquals($expected, $actual, $this->upsertTestCharCast); @@ -2208,7 +2204,7 @@ public function testInsertReturningPksWithQuery(): void { $db = $this->getConnection(true); - $query = (new Query($db))->select([ + $query = $db->select([ 'name' => new Expression("'test_1'"), 'email' => new Expression("'test_1@example.com'"), ]); @@ -2255,8 +2251,7 @@ public function testUpsertReturning( $this->assertEquals($expectedValues, $returnedValues); if (!empty($returnColumns)) { - $selectedValues = (new Query($db)) - ->select(array_keys($expectedValues)) + $selectedValues = $db->select(array_keys($expectedValues)) ->from($table) ->where($selectCondition) ->one(); @@ -2416,8 +2411,7 @@ public function testUuid(): void 'uuid_pk' => $uuidValue, ])->execute(); - $uuid = (new Query($db)) - ->select(['[[uuid_pk]]']) + $uuid = $db->select(['[[uuid_pk]]']) ->from($tableName) ->where(['int_col' => 1]) ->scalar(); @@ -2470,7 +2464,7 @@ public function testJsonTable(): void $this->assertSame('json_col', $tableSchema->getColumn('json_col')->getName()); $this->assertSame(ColumnType::JSON, $tableSchema->getColumn('json_col')->getType()); - $value = (new Query($db))->select('json_col')->from('json_table')->where(['id' => 1])->scalar(); + $value = $db->select('json_col')->from('json_table')->where(['id' => 1])->scalar(); $this->assertSame('{"a":1,"b":2}', str_replace(' ', '', $value)); } } diff --git a/tests/Db/Expression/ArrayExpressionBuilderTest.php b/tests/Db/Expression/ArrayExpressionBuilderTest.php index b2bef34d0..986564ab0 100644 --- a/tests/Db/Expression/ArrayExpressionBuilderTest.php +++ b/tests/Db/Expression/ArrayExpressionBuilderTest.php @@ -71,7 +71,7 @@ public function testBuildQueryExpression(): void $params = []; $builder = new ArrayExpressionBuilder($qb); - $expression = new ArrayExpression((new Query($db))->select('json_field')->from('json_table')); + $expression = new ArrayExpression($db->select('json_field')->from('json_table')); $this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params)); $this->assertSame([], $params); diff --git a/tests/Db/Expression/JsonExpressionBuilderTest.php b/tests/Db/Expression/JsonExpressionBuilderTest.php index cc2181fa0..e7d841011 100644 --- a/tests/Db/Expression/JsonExpressionBuilderTest.php +++ b/tests/Db/Expression/JsonExpressionBuilderTest.php @@ -82,7 +82,7 @@ public function testBuildQueryExpression(): void $params = []; $builder = new JsonExpressionBuilder($qb); - $expression = new JsonExpression((new Query($db))->select('json_field')->from('json_table')); + $expression = new JsonExpression($db->select('json_field')->from('json_table')); $this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params)); $this->assertSame([], $params); diff --git a/tests/Db/Expression/StructuredExpressionBuilderTest.php b/tests/Db/Expression/StructuredExpressionBuilderTest.php index c5fd0d6a0..dfeb66a22 100644 --- a/tests/Db/Expression/StructuredExpressionBuilderTest.php +++ b/tests/Db/Expression/StructuredExpressionBuilderTest.php @@ -81,7 +81,7 @@ public function testBuildQueryExpression(): void $params = []; $builder = new StructuredExpressionBuilder($qb); - $expression = new StructuredExpression((new Query($db))->select('json_field')->from('json_table')); + $expression = new StructuredExpression($db->select('json_field')->from('json_table')); $this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params)); $this->assertSame([], $params); diff --git a/tests/Db/Query/QueryTest.php b/tests/Db/Query/QueryTest.php index 118fe28e8..a96b2f495 100644 --- a/tests/Db/Query/QueryTest.php +++ b/tests/Db/Query/QueryTest.php @@ -31,7 +31,7 @@ public function testColumn(): void 'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute is not supported by this DBMS.' ); - (new Query($db))->select('name')->from('customer')->orderBy(['id' => SORT_DESC])->column(); + $db->select('name')->from('customer')->orderBy(['id' => SORT_DESC])->column(); } public function testCount(): void @@ -43,7 +43,7 @@ public function testCount(): void 'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute is not supported by this DBMS.' ); - (new Query($db))->from('customer')->count(); + $db->select()->from('customer')->count(); } public function testExists(): void @@ -55,14 +55,14 @@ public function testExists(): void 'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute is not supported by this DBMS.' ); - (new Query($db))->from('customer')->where(['status' => 2])->exists(); + $db->select()->from('customer')->where(['status' => 2])->exists(); } public function testLimitOffsetWithExpression(): void { $db = $this->getConnection(true); - $query = (new Query($db))->from('customer')->select('id')->orderBy('id'); + $query = $db->select()->from('customer')->select('id')->orderBy('id'); $query->limit(new Expression('1 + 1'))->offset(new Expression('1 + 0')); $this->expectException(NotSupportedException::class); @@ -82,7 +82,7 @@ public function testOne(): void 'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute is not supported by this DBMS.' ); - (new Query($db))->from('customer')->where(['status' => 2])->one(); + $db->select()->from('customer')->where(['status' => 2])->one(); } public function testColumnWithIndexBy(): void @@ -140,7 +140,7 @@ public function testFor(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = (new Query($db))->for($value); + $query = $db->select()->for($value); assertSame($expected, $query->getFor()); } @@ -149,7 +149,7 @@ public function testForTwice(): void { $db = $this->getConnection(); - $query = (new Query($db))->for('UPDATE'); + $query = $db->select()->for('UPDATE'); $this->expectException(LogicException::class); $this->expectExceptionMessage('The `FOR` part was set earlier. Use the `setFor()` or `addFor()` method.'); @@ -170,7 +170,7 @@ public function testAddFor(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = (new Query($db))->for('NO KEY UPDATE')->addFor($value); + $query = $db->select()->for('NO KEY UPDATE')->addFor($value); assertSame($expected, $query->getFor()); } @@ -189,7 +189,7 @@ public function testAddForOnly(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = (new Query($db))->addFor($value); + $query = $db->select()->addFor($value); assertSame($expected, $query->getFor()); } @@ -208,7 +208,7 @@ public function testSetFor(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = (new Query($db))->for('NO KEY UPDATE')->setFor($value); + $query = $db->select()->for('NO KEY UPDATE')->setFor($value); assertSame($expected, $query->getFor()); } @@ -227,7 +227,7 @@ public function testSetForOnly(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = (new Query($db))->setFor($value); + $query = $db->select()->setFor($value); assertSame($expected, $query->getFor()); } From 8270c547f3ff7a8e553fef8ad6796880b7a14b97 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 12 Jun 2025 06:05:55 +0000 Subject: [PATCH 2/5] Apply fixes from StyleCI --- tests/AbstractConnectionTest.php | 1 - tests/Db/Expression/ArrayExpressionBuilderTest.php | 1 - tests/Db/Expression/JsonExpressionBuilderTest.php | 1 - tests/Db/Expression/StructuredExpressionBuilderTest.php | 1 - 4 files changed, 4 deletions(-) diff --git a/tests/AbstractConnectionTest.php b/tests/AbstractConnectionTest.php index 469a42355..97d073689 100644 --- a/tests/AbstractConnectionTest.php +++ b/tests/AbstractConnectionTest.php @@ -14,7 +14,6 @@ use Yiisoft\Db\Profiler\ContextInterface; use Yiisoft\Db\Profiler\ProfilerInterface; use Yiisoft\Db\Query\BatchQueryResult; -use Yiisoft\Db\Query\Query; use Yiisoft\Db\Tests\Support\Assert; use Yiisoft\Db\Tests\Support\DbHelper; use Yiisoft\Db\Tests\Support\Stub\ColumnFactory; diff --git a/tests/Db/Expression/ArrayExpressionBuilderTest.php b/tests/Db/Expression/ArrayExpressionBuilderTest.php index 986564ab0..10c8d109b 100644 --- a/tests/Db/Expression/ArrayExpressionBuilderTest.php +++ b/tests/Db/Expression/ArrayExpressionBuilderTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\ArrayExpression; use Yiisoft\Db\Expression\ArrayExpressionBuilder; -use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Data\LazyArray; use Yiisoft\Db\Schema\Data\LazyArrayInterface; use Yiisoft\Db\Schema\Data\JsonLazyArray; diff --git a/tests/Db/Expression/JsonExpressionBuilderTest.php b/tests/Db/Expression/JsonExpressionBuilderTest.php index e7d841011..0d199e145 100644 --- a/tests/Db/Expression/JsonExpressionBuilderTest.php +++ b/tests/Db/Expression/JsonExpressionBuilderTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\JsonExpression; use Yiisoft\Db\Expression\JsonExpressionBuilder; -use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Data\LazyArray; use Yiisoft\Db\Schema\Data\JsonLazyArray; use Yiisoft\Db\Schema\Data\StructuredLazyArray; diff --git a/tests/Db/Expression/StructuredExpressionBuilderTest.php b/tests/Db/Expression/StructuredExpressionBuilderTest.php index dfeb66a22..2d09afa99 100644 --- a/tests/Db/Expression/StructuredExpressionBuilderTest.php +++ b/tests/Db/Expression/StructuredExpressionBuilderTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\StructuredExpression; use Yiisoft\Db\Expression\StructuredExpressionBuilder; -use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\AbstractStructuredColumn; use Yiisoft\Db\Schema\Column\ColumnBuilder; use Yiisoft\Db\Schema\Data\JsonLazyArray; From df102a7a97178a40c2af156cfa1a50855a156e87 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 13 Jun 2025 11:47:03 +0700 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Sergei Predvoditelev --- docs/guide/en/schema/typecasting.md | 4 ++-- src/Query/QueryInterface.php | 2 +- tests/AbstractConnectionTest.php | 2 +- tests/Common/CommonCommandTest.php | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/guide/en/schema/typecasting.md b/docs/guide/en/schema/typecasting.md index a9bddd052..5b4f7be88 100644 --- a/docs/guide/en/schema/typecasting.md +++ b/docs/guide/en/schema/typecasting.md @@ -105,7 +105,7 @@ the values when retrieving them from the database. Using these methods, values a they are returned. ```php -$query = $db->select()->from('customer')->where(['id' => 1]); +$query = $db->createQuery()->from('customer')->where(['id' => 1]); $row = $query->withTypecasting()->one(); $isActive = $row['is_active']; @@ -126,7 +126,7 @@ Some databases support date and time types, such as `timestamp`, `datetime`, `da These types are casted to `DateTimeImmutable` objects using `DateTimeColumn` class when type casting is enabled. ```php -$query = $db->select()->from('customer')->where(['id' => 1]); +$query = $db->createQuery()->from('customer')->where(['id' => 1]); $row = $query->withTypecasting()->one(); $createdAt = $row['created_at']; // `DateTimeImmutable` object diff --git a/src/Query/QueryInterface.php b/src/Query/QueryInterface.php index 9ddad9ba9..89ca2c279 100644 --- a/src/Query/QueryInterface.php +++ b/src/Query/QueryInterface.php @@ -306,7 +306,7 @@ public function prepare(QueryBuilderInterface $builder): self; * For example: * * ```php - * $users = $db->select()->from('user') + * $users = $db->createQuery()->from('user') * ->resultCallback(function (array $rows): array { * foreach ($rows as &$row) { * $row['name'] = strtoupper($row['name']); diff --git a/tests/AbstractConnectionTest.php b/tests/AbstractConnectionTest.php index 97d073689..fe41daf59 100644 --- a/tests/AbstractConnectionTest.php +++ b/tests/AbstractConnectionTest.php @@ -36,7 +36,7 @@ public function testCreateBatchQueryResult(): void { $db = $this->getConnection(); - $query = $db->select()->from('customer'); + $query = $db->createQuery()->from('customer'); $this->assertInstanceOf(BatchQueryResult::class, $db->createBatchQueryResult($query)); } diff --git a/tests/Common/CommonCommandTest.php b/tests/Common/CommonCommandTest.php index 9eefef7bb..8f3c763e8 100644 --- a/tests/Common/CommonCommandTest.php +++ b/tests/Common/CommonCommandTest.php @@ -319,7 +319,7 @@ public function testBatchInsert( $command->prepare(false); $command->execute(); - $this->assertEquals($insertedRow, $db->select()->from($table)->count()); + $this->assertEquals($insertedRow, $db->createQuery()->from($table)->count()); $db->close(); } @@ -450,7 +450,7 @@ public function testBatchInsertWithManyData(): void $this->assertSame($attemptsInsertRows, $command->execute()); - $insertedRowsCount = $db->select()->from('{{customer}}')->count(); + $insertedRowsCount = $db->createQuery()->from('{{customer}}')->count(); $this->assertGreaterThanOrEqual($attemptsInsertRows, $insertedRowsCount); @@ -2026,7 +2026,7 @@ public function testUpdate( $this->assertSame($expectedCount, $count); - $values = $db->select() + $values = $db->createQuery() ->from($table) ->where($conditions, $params) ->limit(1) From 2aa289b2dad199c3a456e322d2699c973ffb5dd3 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 13 Jun 2025 11:51:52 +0700 Subject: [PATCH 4/5] Update --- src/Query/QueryInterface.php | 3 ++- tests/Db/Query/QueryTest.php | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Query/QueryInterface.php b/src/Query/QueryInterface.php index 89ca2c279..a24a234f4 100644 --- a/src/Query/QueryInterface.php +++ b/src/Query/QueryInterface.php @@ -306,7 +306,8 @@ public function prepare(QueryBuilderInterface $builder): self; * For example: * * ```php - * $users = $db->createQuery()->from('user') + * $users = $db->createQuery() + * ->from('user') * ->resultCallback(function (array $rows): array { * foreach ($rows as &$row) { * $row['name'] = strtoupper($row['name']); diff --git a/tests/Db/Query/QueryTest.php b/tests/Db/Query/QueryTest.php index a96b2f495..118fe28e8 100644 --- a/tests/Db/Query/QueryTest.php +++ b/tests/Db/Query/QueryTest.php @@ -31,7 +31,7 @@ public function testColumn(): void 'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute is not supported by this DBMS.' ); - $db->select('name')->from('customer')->orderBy(['id' => SORT_DESC])->column(); + (new Query($db))->select('name')->from('customer')->orderBy(['id' => SORT_DESC])->column(); } public function testCount(): void @@ -43,7 +43,7 @@ public function testCount(): void 'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute is not supported by this DBMS.' ); - $db->select()->from('customer')->count(); + (new Query($db))->from('customer')->count(); } public function testExists(): void @@ -55,14 +55,14 @@ public function testExists(): void 'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute is not supported by this DBMS.' ); - $db->select()->from('customer')->where(['status' => 2])->exists(); + (new Query($db))->from('customer')->where(['status' => 2])->exists(); } public function testLimitOffsetWithExpression(): void { $db = $this->getConnection(true); - $query = $db->select()->from('customer')->select('id')->orderBy('id'); + $query = (new Query($db))->from('customer')->select('id')->orderBy('id'); $query->limit(new Expression('1 + 1'))->offset(new Expression('1 + 0')); $this->expectException(NotSupportedException::class); @@ -82,7 +82,7 @@ public function testOne(): void 'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute is not supported by this DBMS.' ); - $db->select()->from('customer')->where(['status' => 2])->one(); + (new Query($db))->from('customer')->where(['status' => 2])->one(); } public function testColumnWithIndexBy(): void @@ -140,7 +140,7 @@ public function testFor(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = $db->select()->for($value); + $query = (new Query($db))->for($value); assertSame($expected, $query->getFor()); } @@ -149,7 +149,7 @@ public function testForTwice(): void { $db = $this->getConnection(); - $query = $db->select()->for('UPDATE'); + $query = (new Query($db))->for('UPDATE'); $this->expectException(LogicException::class); $this->expectExceptionMessage('The `FOR` part was set earlier. Use the `setFor()` or `addFor()` method.'); @@ -170,7 +170,7 @@ public function testAddFor(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = $db->select()->for('NO KEY UPDATE')->addFor($value); + $query = (new Query($db))->for('NO KEY UPDATE')->addFor($value); assertSame($expected, $query->getFor()); } @@ -189,7 +189,7 @@ public function testAddForOnly(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = $db->select()->addFor($value); + $query = (new Query($db))->addFor($value); assertSame($expected, $query->getFor()); } @@ -208,7 +208,7 @@ public function testSetFor(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = $db->select()->for('NO KEY UPDATE')->setFor($value); + $query = (new Query($db))->for('NO KEY UPDATE')->setFor($value); assertSame($expected, $query->getFor()); } @@ -227,7 +227,7 @@ public function testSetForOnly(array $expected, array|string|null $value): void { $db = $this->getConnection(); - $query = $db->select()->setFor($value); + $query = (new Query($db))->setFor($value); assertSame($expected, $query->getFor()); } From 2309308aedeec464ca50047fe125dbe28904497e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 11 Sep 2025 09:25:17 +0000 Subject: [PATCH 5/5] Apply fixes from StyleCI --- tests/AbstractConnectionTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/AbstractConnectionTest.php b/tests/AbstractConnectionTest.php index d4d35dbbb..c5cfb7c9a 100644 --- a/tests/AbstractConnectionTest.php +++ b/tests/AbstractConnectionTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Profiler\ContextInterface; use Yiisoft\Db\Profiler\ProfilerInterface; use Yiisoft\Db\Query\BatchQueryResult; -use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnBuilder; use Yiisoft\Db\Tests\Support\Assert; use Yiisoft\Db\Tests\Support\DbHelper;