I have many hasOne relationships like this:
components
- component_headings
- component_images
Here one Component can only have one hasOne to any of [headings, images].
Logically when a Component.type = heading then it can only have one Heading relation no other relation.
Currently, I'm writing SQL queries like this:
SELECT Component.id, Component.type, Component.`order` FROM Component
LEFT JOIN ComponentHeading ON Component.type = 'heading' AND ComponentHeading.component_id = ContentComponent.id
LEFT JOIN ComponentImage ON Component.type = 'image' AND ComponentImage.component_id = ContentComponent.id
I feel the Query might not be optimized to the best.
Thanks.