amik
amik

Reputation: 5909

Doctrine: querying parent class using fields of descendant classes with multiple table inheritance

I have a hierarchy of user classes using multiple table inheritance. For a specific grid, I need to filter by user names, which are represented differently for different child classes.

In other words, I need to build a DQL query like this:

SELECT u FROM User u WHERE
(u INSTANCEOF CustomerUser
    AND CONCAT ((CustomerUser)u.firstName, ' ', (CustomerUser)u.lastName)
        LIKE :searchString)
OR (u INSTANCEOF InternalUser AND (InternalUser)u.name LIKE :searchString)

Is some kind of type casting like this possible with Doctrine, or is the only way to build a native query?

Thanks in advance.

Upvotes: 1

Views: 113

Answers (1)

lisa
lisa

Reputation: 579

More a workaround than an answer to your exact question, but a solution to simplify your query and speed it up, would be to add a displayName property at the parent level. In the setters of firstName, lastName and name on the child classes, add some code to update it as you want. You'll then be able to:

SELECT u FROM User u WHERE displayName LIKE :searchString

Upvotes: 2

Related Questions