I have a Symfony 4 project and I'm using Doctrine DQL in my repositories to grab data from the database.
I have 4 tables: Initiatives, Organisations, Users and Permissions.
Initiatives table:
id, name, description
Organisations table:
id, name, description
Users table:
id, first_name, last_name
Permissions table:
@int initiative_id - foreign key for Initiatives table
@string reference - the discriminator column. Can have 'organisation' OR 'user' as a value
@int reference_id - the ID of the Organisation OR User
so Organisations and Users are the Entities in the discriminator column.
I want to select only the records where the discriminator column has the values organisation and together with the organisation details from the organisations table.
The select statement looks like this:
SELECT i, ip, o FROM Entity:Initiative i
JOIN i.permissions ip
JOIN ip.organisation o
WHERE ip INSTANCE OF Entity:OrganisationPermission
If I run it will show the error:
InitiativePermission has no association named organisation
InitiativePermission being the entity class for the permissions table.
JOIN ip.organisation o is the problem here....and is normal to see this error because of course we don't have an organization column in permissions table...we have a reference and reference_id which can be organisations or user.
How can I JOIN with the organization table by the reference (discriminator) column ?
Can I do this with DQL or I need another solution ?