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!
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!