diff --git a/docs/guide/en/query-builder.md b/docs/guide/en/query-builder.md index 891fe14c6..400fb263f 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 4556ff215..5db1df6b7 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 07897e6f6..7d1dd0a2c 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 ba2f9a2f6..a5f41574b 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->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 = (new Query($db))->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/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 7fd9dee55..63a8cf3fb 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 f5e865841..d2e10ef83 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 f5919c3fa..bb3ccaf80 100644 --- a/src/Query/QueryInterface.php +++ b/src/Query/QueryInterface.php @@ -313,7 +313,7 @@ public function prepare(QueryBuilderInterface $builder): self; * For example: * * ```php - * $users = (new Query($db)) + * $users = $db->createQuery() * ->from('user') * ->resultCallback(function (array $rows): array { * foreach ($rows as &$row) { diff --git a/tests/Common/CommonCommandTest.php b/tests/Common/CommonCommandTest.php index b52e9177d..dae3156df 100644 --- a/tests/Common/CommonCommandTest.php +++ b/tests/Common/CommonCommandTest.php @@ -507,7 +507,7 @@ public function testBatchInsert( $command->prepare(false); $command->execute(); - $this->assertEquals($insertedRow, (new Query($db))->from($table)->count()); + $this->assertEquals($insertedRow, $db->createQuery()->from($table)->count()); } /** @@ -597,7 +597,7 @@ public function testBatchInsertWithDuplicates(): void $this->assertSame(1, $command->execute()); - $result = (new Query($db)) + $result = $db ->select(['email', 'name', 'address']) ->from('{{customer}}') ->where(['=', '{{email}}', 't1@example.com']) @@ -626,7 +626,7 @@ public function testBatchInsertWithManyData(): void $this->assertSame($attemptsInsertRows, $command->execute()); - $insertedRowsCount = (new Query($db))->from('{{customer}}')->count(); + $insertedRowsCount = $db->createQuery()->from('{{customer}}')->count(); $this->assertGreaterThanOrEqual($attemptsInsertRows, $insertedRowsCount); @@ -726,7 +726,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(); @@ -1360,7 +1360,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], @@ -1404,7 +1404,7 @@ public function testInsertSelect(): void '{{customer}}', ['email' => 't1@example.com', 'name' => 'test', 'address' => 'test address'], )->execute(); - $query = (new Query($db)) + $query = $db ->select(['{{customer}}.{{email}} as name', '{{name}} as email', '{{address}}']) ->from('{{customer}}') ->where(['and', ['<>', 'name', 'foo'], ['status' => [0, 1, 2, 3]]]); @@ -1451,7 +1451,7 @@ public function testInsertSelectAlias(): void 'address' => 'test address', ], )->execute(); - $query = (new Query($db)) + $query = $db ->select(['email' => '{{customer}}.{{email}}', 'address' => 'name', 'name' => 'address']) ->from('{{customer}}') ->where(['and', ['<>', 'name', 'foo'], ['status' => [0, 1, 2, 3]]]); @@ -1989,7 +1989,8 @@ public function testUpdate( $this->assertSame($expectedCount, $count); - $values = (new Query($db)) + $values = $db + ->createQuery() ->from($table) ->where($conditions, $params) ->limit(1) @@ -2148,7 +2149,7 @@ public function testInsertReturningPksWithQuery(): void $db = $this->getSharedConnection(); $this->loadFixture(); - $query = (new Query($db))->select([ + $query = $db->select([ 'name' => new Expression("'test_1'"), 'email' => new Expression("'test_1@example.com'"), ]); @@ -2212,7 +2213,7 @@ public function testUpsertReturning( $this->assertEquals($expectedValues, $returnedValues); if (!empty($returnColumns)) { - $selectedValues = (new Query($db)) + $selectedValues = $db ->select(array_keys($expectedValues)) ->from($table) ->where($selectCondition) @@ -2384,7 +2385,7 @@ public function testUuid(): void 'uuid_pk' => $uuidValue, ])->execute(); - $uuid = (new Query($db)) + $uuid = $db ->select(['[[uuid_pk]]']) ->from($tableName) ->where(['int_col' => 1]) @@ -2432,7 +2433,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)); $db->close(); diff --git a/tests/Common/CommonConnectionTest.php b/tests/Common/CommonConnectionTest.php index 9bcd44279..a1cddaadc 100644 --- a/tests/Common/CommonConnectionTest.php +++ b/tests/Common/CommonConnectionTest.php @@ -13,7 +13,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\IntegrationTestCase; @@ -25,7 +24,7 @@ public function testCreateBatchQueryResult(): void { $db = $this->getSharedConnection(); - $query = (new Query($db))->from('customer'); + $query = $db->createQuery()->from('customer'); $this->assertInstanceOf(BatchQueryResult::class, $db->createBatchQueryResult($query)); } diff --git a/tests/Db/Expression/Value/Builder/ArrayValueBuilderTest.php b/tests/Db/Expression/Value/Builder/ArrayValueBuilderTest.php index 63b4a0189..ad65db47c 100644 --- a/tests/Db/Expression/Value/Builder/ArrayValueBuilderTest.php +++ b/tests/Db/Expression/Value/Builder/ArrayValueBuilderTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\Value\Builder\ArrayValueBuilder; -use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Data\LazyArray; use Yiisoft\Db\Schema\Data\LazyArrayInterface; use Yiisoft\Db\Schema\Data\JsonLazyArray; @@ -69,7 +68,7 @@ public function testBuildQueryExpression(): void $params = []; $builder = new ArrayValueBuilder($qb); - $expression = new ArrayValue((new Query($db))->select('json_field')->from('json_table')); + $expression = new ArrayValue($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/Value/Builder/JsonValueBuilderTest.php b/tests/Db/Expression/Value/Builder/JsonValueBuilderTest.php index 9a92b6dfd..b7fd637c3 100644 --- a/tests/Db/Expression/Value/Builder/JsonValueBuilderTest.php +++ b/tests/Db/Expression/Value/Builder/JsonValueBuilderTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\Value\JsonValue; use Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder; -use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Data\LazyArray; use Yiisoft\Db\Schema\Data\JsonLazyArray; use Yiisoft\Db\Schema\Data\StructuredLazyArray; @@ -80,7 +79,7 @@ public function testBuildQueryExpression(): void $params = []; $builder = new JsonValueBuilder($qb); - $expression = new JsonValue((new Query($db))->select('json_field')->from('json_table')); + $expression = new JsonValue($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/Value/Builder/StructuredValueBuilderTest.php b/tests/Db/Expression/Value/Builder/StructuredValueBuilderTest.php index 73bc967b4..de1308cb7 100644 --- a/tests/Db/Expression/Value/Builder/StructuredValueBuilderTest.php +++ b/tests/Db/Expression/Value/Builder/StructuredValueBuilderTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\Value\StructuredValue; use Yiisoft\Db\Expression\Value\Builder\StructuredValueBuilder; -use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\AbstractStructuredColumn; use Yiisoft\Db\Schema\Column\ColumnBuilder; use Yiisoft\Db\Schema\Data\JsonLazyArray; @@ -79,7 +78,7 @@ public function testBuildQueryExpression(): void $params = []; $builder = new StructuredValueBuilder($qb); - $expression = new StructuredValue((new Query($db))->select('json_field')->from('json_table')); + $expression = new StructuredValue($db->select('json_field')->from('json_table')); $this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params)); $this->assertSame([], $params);