如何在原则 2 中进行复杂的实体关联查询? (虚拟实体)
假设我有一个博客应用程序。
作者可以通过提供网络上现有图像的 URL 链接或上传新图像来将多个图像添加到帖子中。
我希望能够从图像(上传或网址)回溯到使用该图像的所有帖子,以及从特定帖子回溯到该帖子中的所有图像(例如 - 这样我就可以删除非法图像和/或暂停使用它们的帖子,直到作者修复该帖子)。
但是,我对上传的图像和 url 图像使用不同的实体 - 上传的图像比 url 图像具有更多有关图像的数据。
这会导致代码更加复杂,因为在每次保存编辑后的帖子时,我需要检查特定图像是哪种类型(在我需要运行的帖子中的所有图像中),然后创建或更新其记录,并且分配给帖子实体中的关系字段。
所以...假设我有一个 UploadedImage
和一个 UrlImage
实体,我考虑在 post
中有一个 setImages
方法code> 实体,它检查它获取的每个图像是否已上传或 url,然后调用 setUploadedImages
或 setUrlImages
。
但是,我需要某种虚拟images
存储库,以相同的方式加载图像。
这听起来有点复杂,我想知道:
- 我怎样才能拥有一个虚拟学说实体(及其存储库和所有内容)?
- 有没有更好的设计呢?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是您想要执行的操作的文档:
http://doctrine-orm.readthedocs.org/en/latest /reference/inheritance-mapping.html
它描述了单表继承与类表继承。
您的情况适合后一种情况,但也请考虑迁移到单表继承。
如果这些图像的(代码)用户无论其来源如何都对它们一视同仁,那么增加的查询复杂性和数据库使用将不值得标准化。
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.