简体   繁体   English

如何在学说2中进行复杂的实体关联查询? (虚拟实体)

[英]How can I do complex entity associations queries in doctrine 2? (virtual entity)

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. 作者可以通过提供指向网络上现有图像的URL的链接或者上传新图像来向帖子添加多个图像。

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. 这导致更复杂的代码,因为在每次保存编辑的帖子时我需要检查哪种类型是特定图像(在我需要运行的帖子中的所有图像中),而不是创建或更新其记录,以及分配给post实体中的关系字段。

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 . 所以...假设我有一个UploadedImage和一个UrlImage实体,我想在post实体中有一个setImages方法,它会检查它获得的每个图像,如果它是上传的或url,然后调用setUploadedImagessetUrlImages

However, I will need some kind of a virtual images repository, to load images in the same way. 但是,我需要某种虚拟images存储库,以相同的方式加载图像。

It sounds a bit complex, and I wondered: 听起来有点复杂,我想知道:

  1. How can I have a virtual doctrine entity (with its repository and all) ? 我如何拥有一个虚拟学说实体(包含其存储库和所有实体)?
  2. 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 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. 如果(代码)这些图像的用户无论其来源如何都将它们视为相同,则添加的查询复杂性和数据库使用将不值得标准化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM