I use Symfony2 with Doctrine2 (latest versions) and define this relationship:
/** * @ORM\OneToMany(targetEntity="Field", mappedBy="event", fetch="EAGER") * @ORM\OrderBy({"name" = "ASC"}) */ protected $fields;
The other side of the relationship is defined as:
/** * @ORM\ManyToOne(targetEntity="Event", inversedBy="fields", fetch="EAGER") * @ORM\JoinColumn(nullable=false, onDelete="CASCADE") */ protected $event;
When executing "fetchOnyById", Doctrine runs 2 queries. 1 to retrieve the object itself and 1 for related fields. I expect this to be a union, but it is not.
When this is done in the controller, I pass my object to the branch. There I again return the fields as a property of the object. This forces another query to be run to retrieve the fields.
It is clear that I am doing something wrong, as I expect only one request to be launched, and 3 are really running.
source share