0

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?

Kurzyx
  • 372
  • 3
  • 14
  • If you use table inheritance (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#class-table-inheritance) can you not use the "INSTANCE OF" in DQL (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples)??? – Alex.Barylski Jan 04 '18 at 17:02
  • That appears to only work for filtering. – Kurzyx Jan 05 '18 at 14:31

0 Answers0