diff --git a/CHANGELOG.md b/CHANGELOG.md index d391a4164..614264c5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ - Enh #855: Add array and JSON overlaps conditions (@Tigrov) - Enh #860: Add `bit` abstract type (@Tigrov) - Enh #862: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov) +- Enh #798: Allow `QueryInterface::one()` and `QueryInterface::all()` to return objects (@darkdef, @Tigrov) ## 1.3.0 March 21, 2024 diff --git a/UPGRADE.md b/UPGRADE.md index d8767e8ae..bbce1e55a 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -126,4 +126,6 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace ### Other changes -- Allow `ExpressionInterface` for `$alias` parameter of `QueryPartsInterface::withQuery()` method +- Allow `ExpressionInterface` for `$alias` parameter of `QueryPartsInterface::withQuery()` method; +- Allow `QueryInterface::one()` to return an object; +- Allow `QueryInterface::all()` to return array of objects; diff --git a/src/Command/CommandInterface.php b/src/Command/CommandInterface.php index 57bcc5828..a10748f90 100644 --- a/src/Command/CommandInterface.php +++ b/src/Command/CommandInterface.php @@ -623,7 +623,7 @@ public function query(): DataReaderInterface; * @throws Exception * @throws Throwable If execution failed. * - * @return array[] All rows of the query result. Each array element is an array representing a row of data. + * @return array[] All rows of the query result. Each array element is an `array` representing a row of data. * Empty array if the query results in nothing. */ public function queryAll(): array; @@ -649,8 +649,7 @@ public function queryColumn(): array; * @throws Exception * @throws Throwable If execution failed. * - * @return array|null The first row (in terms of an array) of the query result. Null if the query - * results in nothing. + * @return array|null The first row as an `array` of the query result. `null` if the query results in nothing. */ public function queryOne(): array|null; diff --git a/src/Query/Query.php b/src/Query/Query.php index 60d635159..0c6921cbc 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -531,7 +531,7 @@ public function offset(ExpressionInterface|int|null $offset): static return $this; } - public function one(): array|null + public function one(): array|object|null { return match ($this->emulateExecution) { true => null, diff --git a/src/Query/QueryInterface.php b/src/Query/QueryInterface.php index 04cddbd60..75ed79905 100644 --- a/src/Query/QueryInterface.php +++ b/src/Query/QueryInterface.php @@ -51,7 +51,8 @@ public function addParams(array $params): static; * @throws InvalidConfigException * @throws Throwable * - * @return array[] The query results. If the query results in nothing, it returns an empty array. + * @return array[]|object[] All rows of the query result. Each array element is an `array` or `object` representing + * a row of data. Empty array if the query results in nothing. */ public function all(): array; @@ -258,10 +259,10 @@ public function getWithQueries(): array; * @throws InvalidConfigException * @throws Throwable * - * @return array|null The first row (in terms of an array) of the query result. It returns `null` if the query + * @return array|object|null The first row as an `array` or as an `object` of the query result. `null` if the query * results in nothing. */ - public function one(): array|null; + public function one(): array|object|null; /** * Sets the parameters to bind to the query.