Let's say I have a blog application.
The author can add multiple images to a post either by giving a link to a url of an existing image on the web, or upload a new image.
I want to be able to back track from an image (either uploaded or url) to all the posts that uses that image, and from a specific post to all the images in that post (for example - so I could delete images that are illegal and/or suspend the post that uses them until the author fixes the post).
However, I use different entities for an uploaded image vs a url image - an uploaded image has more data regarding the image than a url image.
This results in more complex code, as in each saving of an edited post I need to check which kind is the specific image (out of all the images in the post I need to run over), and than create or update its record, and assign to the relation field in the post entity.
So... assuming I have an UploadedImage
and a UrlImage
entities, I thought about having a setImages
method in the post
entity, which checks per each image it gets, if it's uploaded or url, and then calls either setUploadedImages
or setUrlImages
.
However, I will need some kind of a virtual images
repository, to load images in the same way.
It sounds a bit complex, and I wondered:
- How can I have a virtual doctrine entity (with its repository and all) ?
- Is there a better design for it ?
Here's the documentation for what you are trying to do:
http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html
It describes single table inheritance vs class table inheritance. Your case is suited for the latter one, but consider migrating to the single table inheritance as well.
If (code) users of these images treat them the same regardless of their origin, added query complexity and database usage will not be worth the normalization.