Skip to content

AutoMapper does not handle Doctrine managed entities, leading to new instances being created for existing entities #198

@m4n50n

Description

@m4n50n

I am using AutoMapper in a Symfony project alongside Doctrine. When mapping a DTO to an entity, AutoMapper creates new instances for related entities, even if those entities already exist in the database and should be managed by Doctrine.

For example, I am mapping a Course DTO that contains a CourseType (which is an existing entity in the database). The CourseType has a valid id, and this should indicate to Doctrine that it is an existing entity, but AutoMapper is not respecting that. Instead, it creates a new CourseType instance, which is not managed by Doctrine, leading to persistence issues (e.g., Doctrine attempts to persist the new CourseType as if it were a new entity).

The only way I found to ensure that the related entity is managed by Doctrine is to manually fetch it from the database before persisting the mapped entity, which defeats the purpose of using AutoMapper to simplify this process.

What can I do? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions