Skip to content

Problem mapping an entity to a DTO #204

Open
@SalvadorCardona

Description

@SalvadorCardona

Hello,

I has a problem with Auto Mappers. I try to map my Entity to a Dto.

My context

  • Api Platform 3.2
  • Symfony 7.1
  • Automapper 9.1

Debuger

image

Auto Mapper Config


automapper:
    class_prefix: "Symfony_Mapper_"
    constructor_strategy: 'auto'
    check_attributes: true
    auto_register: true
    map_private_properties: true
    allow_readonly_target_to_populate: false
    normalizer:
        enabled: true
        only_registered_mapping: false
        priority: 1000
    loader:
        eval: false
        cache_dir: "%kernel.cache_dir%/automapper"
        reload_strategy: "always"
    serializer_attributes: true
    api_platform: false
    name_converter: null
    mapping:
        paths:
            - '%kernel.project_dir%/src'
     

My DTO


<?php

declare(strict_types=1);

namespace App\Work\Profile\ElasticSearch\DTO;

use App\Shared\Address\Entity\Address;

class ProfileEs
{
    public ?string $firstName = null;
    public ?string $phone = null;
    public ?string $email = null;

    /**
     * @var array
     */
    public array $profileSkills = [];

    public ?Address $address = null;
}

My Entity

#[Mapper\Mapper(target: [ProfileEs::class])]
class Profile extends AbstractBaseEntity
{
    #[ORM\Column(nullable: true)]
    #[Groups(['profile:read:item', 'profile:read:collection'])]
    #[MapTo(target: [ProfileEs::class, 'array'], property: 'firstName')]
    public ?string $firstName = null;

    #[ORM\Column(nullable: true)]
    #[Groups(['profile:read:item', 'profile:read:collection'])]
    public ?string $lastName = null;

    /** @var Collection<array-key, ProfileSkill> */
    #[ORM\OneToMany(
        mappedBy: 'profile',
        targetEntity: ProfileSkill::class,
        cascade: ['persist', 'remove'],
        orphanRemoval: true
    )]
    public Collection $profileSkills;

My Service


<?php

declare(strict_types=1);

namespace App\Work\Profile\ElasticSearch;

use App\Work\Profile\ElasticSearch\DTO\ProfileEs;
use App\Work\Profile\Entity\Profile\Profile;
use AutoMapper\AutoMapperInterface;

final class ProfileToEsDtoTransformer
{
    public function __construct(private AutoMapperInterface $autoMapper)
    {
    }

    public function toDto(Profile $profile): ProfileEs
    {
        $target = $this->autoMapper->map($profile, ProfileEs::class);

        return $target;
    }
}

Result

image

On the result of mapper, i can see that property profileSkills it was well mapped but the other property like firstName or phone or email is not weel mapped. Their values ​​are zero in the result.

I try to implement this solution for pushing data on Elastic Search.

Thank You for your future response.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions