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 for it.

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

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

Converting to single Object

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:

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.

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

Last updated

Was this helpful?