Skip to content

Support for Nested Property Mapping for Array to Object mapping #176

@TheoD02

Description

@TheoD02

Description

I would like to map using nested properties for array to object mapping in the current implementation.

Example

Given an input array:

[
    'saved_at' => '2024-01-01',
    'track' => [
        'uri' => 'uri-code',
     ]
]

And the following entity definition:

#[ORM\Entity(repositoryClass: TrackRepository::class)]
class Track
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[MapFrom(source: 'array', property: 'track.uri')] // tested with [track][uri] like PropertyAccessor
    #[ORM\Column(length: 100)]
    private ?string $uri = null;

    // ... other properties and methods
}

Issue

Currently, when attempting to map the nested track.uri property to the Track entity's uri property, the generated mapping does not appear to handle nested properties as expected. The generated code is:

if (array_key_exists('track.uri', $value) && \AutoMapper\MapperContext::isAllowedAttribute($context, 'track.uri', !isset($value['track.uri'])) && (!array_key_exists('groups', $context) || !$context['groups'])) {
    $result->setUri($value['track.uri']);
}

The code attempts to access track.uri as a single key in the array, instead of navigating through the nested structure.

Expected Behavior

The mapping should properly navigate the nested array structure and map the track.uri value to the uri property of the Track entity. For instance:

if (isset($value['track']['uri']) && \AutoMapper\MapperContext::isAllowedAttribute($context, 'track.uri', !isset($value['track']['uri'])) && (!array_key_exists('groups', $context) || !$context['groups'])) {
    $result->setUri($value['track']['uri']);
}

Questions

  1. Is it possible to support nested properties for array to object mapping?
  2. If not currently supported, are there plans to implement this feature?

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions