如何将一个Doctrine实体保存到两个数据库表(MySQL优化所需) [英] How to save one Doctrine entity to two database tables (required for MySQL optimisation)

查看:147
本文介绍了如何将一个Doctrine实体保存到两个数据库表(MySQL优化所需)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中,有一个表 file 和一个表 file_content file 表存储文件的元数据,例如 name mime 等。 file_content 存储带有文件内容的Blob。我不会将blob与元数据存储在同一表中,仅出于性能方面的考虑。

In my database I've a table file and a table file_content. The file table stores the metadata of the file such as name, mime and some more. The file_content stores a blob with the file content. I'm not storing the blob in the same table as the metadata for performance reasons only.

对于项目的版本2,我正在研究Doctrine( 2.3)。对我来说,文件似乎是一个实体,具有诸如 name mime 扩展内容,应按以下方式使用:

For a 'version 2' of my project I'm looking into Doctrine (2.3). To me, a "File" seems to be one entity, with properties such as name, mime, extension, content that should be used like this:

$file = new File();
$file->setName('hello.txt');
$file->setMime('text/plain');
$file->setContent('Hello world!')
$em->persist($file);
$em->flush();

这种行为可能吗?对我来说,为实际上只是一个实体的东西创建两个实体是没有意义的。我在文档中找不到任何相关内容,并且阅读了2年的主题,它在Doctrine 2.1中是不可能的:原理2.1-将实体映射到多个表

Is this behaviour possible? To me it makes no sense to create two entities for something that's really just one entity. I could not find anything about it in the documentation and I read in a 2-year-old topic that it isn't possible in Doctrine 2.1: Doctrine 2.1 - Map entity to multiple tables

有人建议如何正确处理此问题?我是Doctrine的新手,并且一直在研究它,以查看它是否适合我的项目。谢谢。

Someone any suggestions how to handle this correctly? I'm new to Doctrine and have been playing around with it a bit to see if it's the right choice for my project. Thanks.

推荐答案

您有能力更改数据库的架构吗?如果是这样,我考虑将其合并到一个表中。

Do you have the ability to alter the schema of your database? If so, I'd consider consolidating this into one table.

除非如此,否则您可能想尝试在教义中进行一对一的关系。也许像这样:

Barring that, you may want to try a one-to-one relationship in Doctrine. Perhaps something like:

class File {
    private $id;
    private $name;
    private $mime;
    private $content;
}

class Content {
    private $id;
    private $data;
    private $fileId;
}

如果您将Content-> fileId与一对一关系映射到File-> id,那么您可以执行以下操作:

If you map Content->fileId with a one-to-one relationship to File->id, then you can do things like:

$file->getContent()->getData();
$file->getContent()->setData("something different");

有关一对一映射的更多信息: http://docs.doctrine-project.org/en/ 2.0.x / reference / association-mapping.html#one-to-one-unidirectional

Here's some more info on one-to-one mappings: http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#one-to-one-unidirectional

这篇关于如何将一个Doctrine实体保存到两个数据库表(MySQL优化所需)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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