Doctrine inheritance and associations

I have entity Image with properties:

class Image {   

private $id
private $name;
private $mimeType;
private $size;  }

Now, I need to add new image type which will have all properties as existing class and some new properties like author name, external url, url to author… Let’s call it ExternalImage. Existing images don’t need these properties.

Then I need to add image property to Event class and it can be either Image or ExternalImage.

One other use case is that I need to list all images (both types at once, on some API endpoint).

My solution would be:

ExternalImage extends Image
 .. add new properties

and use SINGLE TABLE Doctrine’s inheritance. And then add OneToOne association to Event class (adding image_id property).

<one-to-one field="image" target-entity="AppCommonEntityImage">
  1. is inheritance appropriate for this example? I don’t see any other solution from my perspective. We have strong "is a" relation between Image and ExternalImage.
  2. I am aware of performance issue with using STI entity as target entity of one-to-one association – image entity won’t be lazily loaded when I fetch Event object. Can I do something about that?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *