1

如何将文档内的字段映射为关联数组,其值是对另一个文档的引用?

假设我有一个文件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

4

1 回答 1

0

对 Doctrine 的 ArrayCollection 使用“设置”策略

/** @Document */
class Image {

    /** @Id */
    protected $id;

    /**
     * @ReferenceMany(targetDocument="File", strategy="set") 
     */

    protected $files;

    public function setFile($resolution, File $file)
    {
        $this->files[$resolution] = $file;
    }

    // Getter and setters omitted
}
于 2013-10-04T15:00:45.240 回答