I am trying to eagerly load associated entities defined in a subclass of an association (in DQL) so that this association is not lazy-loaded instead (which caused many individual queries.)
Example situation
Suppose I have a business with people (which I will call persons for simplicity) of which several are employees. An employee can have books in its possession while all other persons can not.
/**
* @Entity
*/
class Business
{
/**
* @OneToMany(targetEntity="Person", mappedBy="business")
*/
private $persons;
}
/**
* @Entity
* @InhertianceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
/**
* @ManyToOne(targetEntity="business", inversedBy="persons")
* @JoinColumn(name="business_id", referencedColumnName="id")
*/
private $business;
}
/**
* @Entity
*/
class Employee extends Person
{
/** ... */
private $books;
}
/**
* @Entity
*/
class Book { /* ... */ }
Entities representing the situation in PHP.
Problem
I want to obtain a business entity and all its persons while also eagerly loading the books of all employees so that they aren't lazy-loaded for each indivudual employee.
If I were to do this via a DQL query I would have to down-cast the Person association to Employee in order to join the books. This is however not possible in DQL. (see relevant issue about this topic)
Possible non-working example in DQL:
SELECT
business, employee, employeeBook
JOIN
CAST(business.persons AS Entity\Employee) AS employee
JOIN
employee.books AS employeeBook
Since this is a non-working example... How would/should I do this and how?