doctrine-relations
Installation
SKILL.md
Doctrine Relations (Symfony)
Use when
- Mapping a
ManyToOne,OneToMany,ManyToManyorOneToOneand choosing the owning side. - Deciding between
cascadeandorphanRemovalfor child entities. - A relation does not persist (only the inverse side was set), or
contains()is slow on a large inverse collection. - Splitting entities across several entity managers.
Default workflow
- Pick the owning side (holds the foreign key,
ManyToOnewithinversedBy) and the inverse side (OneToManywithmappedBy). - Initialize collections with
ArrayCollectionin the constructor, and writeaddX()andremoveX()helpers that keep both sides in sync. - Use
cascade: ['persist']for aggregates saved together, andorphanRemoval: trueonly for true composition. - Mark large inverse collections
fetch: 'EXTRA_LAZY', or test membership from the owning side. - Add fetch joins where a use case reads the relation, to avoid N+1 queries.
- With several entity managers, keep each entity set in one manager and fetch repositories through
ManagerRegistry.