Technical Mid Level

Explain the different relationship types in Doctrine ORM. When do you use unidirectional versus bidirectional associations, and what are the performance implications?

Quick Tip

Show the decision: "I use ManyToOne unidirectional when I only need to traverse one direction. Bidirectional adds convenience but also responsibility — I must maintain both sides in code and be aware of cascade implications."

What good answers include

Doctrine supports OneToOne, OneToMany, ManyToOne, and ManyToMany. Unidirectional associations only define the relationship on one side — simpler but you cannot traverse from the inverse side. Bidirectional requires an owning side (where the foreign key lives) and an inverse side (mappedBy). Performance: bidirectional ManyToMany creates a join table and can lead to large hydrations; consider replacing with a linking entity for extra columns. Lazy loading is the default — collections are loaded on first access. Strong candidates discuss: cascade operations (persist, remove), fetch modes (LAZY, EAGER, EXTRA_LAZY), orphanRemoval for cleanup, and the cost of hydrating large collections versus using DQL pagination.

What interviewers are looking for

Core Doctrine knowledge. Candidates who cannot explain the owning side concept will create inconsistent data. Those who understand EXTRA_LAZY for large collections and orphanRemoval for cleanup demonstrate production experience.

← All Symfony questions