我该怎么办教义2复杂的实体关联查询? (虚拟实体) [英] How can I do complex entity associations queries in doctrine 2? (virtual entity)

查看:161
本文介绍了我该怎么办教义2复杂的实体关联查询? (虚拟实体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个博客应用程序。结果
作者可以多个图像后无论是在网络上给人一种链接到现有图像的URL添加,或上传新的​​图片。

我希望能够从图像备份轨道(无论是上传或URL)到使用该图像的所有帖子,并从具体职位的所有图像在该职位(例如 - 这样我就可以删除图像这是非法的和/或暂停使用它们的岗位直到笔者修正后)。

不过,我用不同的实体上传图像VS一个URL图像 - 上传的图像有关于不是URL图像的图像更多的数据。

这将导致更复杂的code,在经过编辑后的每节省我需要检查哪一种是具体的图像(出在后的所有图像,我需要跑过来),比创建或更新记录,并分配给相关领域在后的实体。

所以...假设我有一个 UploadedImage UrlImage 的实体,我想到有一个 setImages 实体,每它得到,如果它的上载或URL,然后每个图像哪些检查方法调用要么 setUploadedImages setUrlImages

不过,我会需要某种形式的虚拟图片资料库,以同样的方式来加载图像。

这听起来有点复杂,我想知道:


  1. 怎样才可以有一个虚拟的学说实体(有资料库和所有)?

  2. 是否有一个更好的设计?


解决方案

下面是什么你正在尝试做的文档:结果
http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html

据介绍单表继承VS类表继承。
你的情况适合于后者,但考虑迁移到单一表继承以及

如果(code)这些图像的用户对待他们一样的,不管其出身,增加查询的复杂性和数据库的使用不值得正常化。

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:

  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

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.

这篇关于我该怎么办教义2复杂的实体关联查询? (虚拟实体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆