Coalesce in JoinColumn #5530

Closed
opened 2026-01-22 15:10:13 +01:00 by admin · 3 comments
Owner

Originally created by @michsk on GitHub (May 9, 2017).

Originally assigned to: @Ocramius on GitHub.

Do we at this moment have a way to use coalesce in the join column for relations?

For example a car has many parts. A car can have a relation to a car. I want to make demo cars, which refer to another car (the original car). But i do not want to copy all the parts of the car. So i would like to join using Coalesce. join part.car_id = coalesce(car.car_id, car.id) So we would use the coalesce to first check if the car has a car_id, referring to the original car, if so, its a demo car and we use the car id of the original car for loading the parts. If not than use the car's own id. For example when calling the original car.

If this would not be possible using the Annotations. Would it be possible in a different way?

Originally created by @michsk on GitHub (May 9, 2017). Originally assigned to: @Ocramius on GitHub. Do we at this moment have a way to use coalesce in the join column for relations? For example a car has many parts. A car can have a relation to a car. I want to make demo cars, which refer to another car (the original car). But i do not want to copy all the parts of the car. So i would like to join using Coalesce. `join part.car_id = coalesce(car.car_id, car.id)` So we would use the coalesce to first check if the car has a car_id, referring to the original car, if so, its a demo car and we use the car id of the original car for loading the parts. If not than use the car's own id. For example when calling the original car. If this would not be possible using the Annotations. Would it be possible in a different way?
admin added the ImprovementWon't FixQuestion labels 2026-01-22 15:10:13 +01:00
admin closed this issue 2026-01-22 15:10:14 +01:00
Author
Owner

@michsk commented on GitHub (May 9, 2017):

Maybe have DemoCar, and use the Overwrite annotation to overwrite the join column? Guess that would be the best way to do this.

@michsk commented on GitHub (May 9, 2017): Maybe have DemoCar, and use the Overwrite annotation to overwrite the join column? Guess that would be the best way to do this.
Author
Owner

@Ocramius commented on GitHub (May 9, 2017):

@Michal-sk I don't understand the scenario: what would this look like, in entities?
Also, maybe I'm naive, but I've never seen a coalesce join. The ORM always builds associations towards identifiers only, so car_id, being a foreign key, and not an identifier, is an invalid referenced column.

@Ocramius commented on GitHub (May 9, 2017): @Michal-sk I don't understand the scenario: what would this look like, in entities? Also, maybe I'm naive, but I've never seen a coalesce join. The ORM always builds associations towards identifiers only, so `car_id`, being a foreign key, and not an identifier, is an invalid referenced column.
Author
Owner

@michsk commented on GitHub (May 10, 2017):

The ORM always builds associations towards identifiers only, so car_id, being a foreign key, and not an identifier, is an invalid referenced column.

Got it.

@michsk commented on GitHub (May 10, 2017): > The ORM always builds associations towards identifiers only, so car_id, being a foreign key, and not an identifier, is an invalid referenced column. Got it.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5530