如何将文档内的字段映射为关联数组,其值是对另一个文档的引用?
假设我有一个文件File
,它代表磁盘上的某个文件。像这样的东西:
/** @Document */
class File {
/** @Id */
protected $id;
/** @String */
protected $filename;
// Getter and setters omitted
}
另一个代表图像的文档,其中存储了对不同大小图像的引用。像这样的东西:
/** @Document */
class Image {
/** @Id */
protected $id;
/** ???? */
protected $files;
// Getter and setters omitted
}
我现在希望能够在图像文档中存储一些对文件的引用,这些文件以它们的大小为关键字。例如:
$file1 = new File('/some/path/to/a/file');
$file2 = new File('/some/path/to/another/file');
$image = new Image();
$image->setFiles(array('50x50' => $file1,'100x100' => $file2));
生成的 MongoDB 文档应如下所示:
{
"_id" : ObjectId("...."),
"files" : {
"50x50" : {
"$ref" : "files",
"$id" : ObjectId("...")
},
"100x100" : {
"$ref" : "files",
"$id" : ObjectId("...")
}
}
}
那么如何映射文档files
中的字段呢?Image