Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem mapping an entity to a DTO #204

Open
SalvadorCardona opened this issue Nov 7, 2024 · 0 comments
Open

Problem mapping an entity to a DTO #204

SalvadorCardona opened this issue Nov 7, 2024 · 0 comments

Comments

@SalvadorCardona
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant