> For the complete documentation index, see [llms.txt](https://docs.ecotone.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ecotone.tech/modelling/command-handling/business-interface/working-with-database/converting-results.md).

# Converting Results

## Converting Results

In rich business domains we will want to work with higher level objects than associate arrays.\
Suppose we have PersonNameDTO and defined [Ecotone's Converter](/messaging/conversion/conversion.md) for it.

```php
class PersonNameDTOConverter
{
    #[Converter]
    public function from(PersonNameDTO $personNameDTO): array
    {
        return [
            "person_id" => $personNameDTO->getPersonId(),
            "name" => $personNameDTO->getName()
        ];
    }

    #[Converter]
    public function to(array $personNameDTO): PersonNameDTO
    {
        return new PersonNameDTO($personNameDTO['person_id'], $personNameDTO['name']);
    }
}
```

### Converting to Collection of Objects

```php
/**
* @return PersonNameDTO[]
*/
#[DbalQuery('SELECT person_id, name FROM persons LIMIT :limit OFFSET :offset')]
public function getNameListDTO(int $limit, int $offset): array;
```

Ecotone will read the Docblock and based on that will deserialize *Result Set from database* to list of *PersonNameDTO*.

### Converting to single Object

```php
#[DbalQuery(
    'SELECT person_id, name FROM persons WHERE person_id = :personId',
    fetchMode: FetchMode::FIRST_ROW
)]
public function getNameDTO(int $personId): PersonNameDTO;
```

Using combination of First Row Fetch Mode, we can get first row and then use it for conversion to PersonNameDTO.

## Converting Iterator

For big result set we may want to avoid fetching everything at once, as it may consume a lot of memory. In those situations we may use *Iterator Fetch Mode*, to fetch one by one.\
If we want to convert each result to given Class, we may define docblock describing the result:

<pre class="language-php"><code class="lang-php">/**
 * @return iterable&#x3C;PersonNameDTO>
 */
<strong>#[DbalQuery(
</strong>    'SELECT person_id, name FROM persons ORDER BY person_id ASC',
    fetchMode: FetchMode::ITERATE
)]
public function getPersonIdsIterator(): iterable;
</code></pre>

Each returned row will be automatically convertered to *PersonNameDTO*.

## Converting to specific Media Type Format

We may return the result in specific format directly. This is useful when Business Method is used on the edges of our application and we want to return the result directly.

```php
#[DbalQuery(
    'SELECT person_id, name FROM persons WHERE person_id = :personId',
    fetchMode: FetchMode::FIRST_ROW,
    replyContentType: 'application/json'
)]
public function getNameDTOInJson(int $personId): string;
```

In this example, result will be returned in *application/json*.\\


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.ecotone.tech/modelling/command-handling/business-interface/working-with-database/converting-results.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
