I have this schema:
Entity Doctrine-generated Entity
+++++++++ ++++++++++++++++ +++++++
+Student+ ----- +Student/Events+ ------ +Event+
+++++++++ ++++++++++++++++ +++++++
What this says is that a Student can have many events, and is represented with a ManyToMany relationship. The Student is the owning side of the relationship.
Each Event
is added with an incremental id
and associated to a Student
. Of course, the mapping table is generated automatically by doctrine due to the ManyToMany
.
I'm using event sourcing
for this, so each Student
has a status
, that status is the Student's last event.
Now I would like to fetch all Students
that have a given status, say, for instance, "IN_REVIEW". Remember that the status is represented by the Student's last event.
¿How can I do this with doctrine in just one trip to the database? More specifically, I would like to do this with a query builder.
PD: I have the following code:
$qb->select('a')
->from("DnDRaHApiBundle:Student", "a")
->leftJoin("a.status", "s");
But can't figure out how to query on the mapping table. I've already thought on fetching all Events
of a Student
in reverse id order, then use that to query for a Student, but I don't like that approach, there has to be a better way.