mysql 将多个表保存在哪里,如何将一个Doctrine实体保存到两个数据库表(MySQL优化所需)...

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.

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();

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

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;

}

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");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值