保存带有引用的关联数组作为doctrine-mongodb中的值

时间:2013-01-25 10:23:35

标签: php doctrine mongodb-php doctrine-mongodb

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

假设我有一个文档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字段?

1 个答案:

答案 0 :(得分:0)

将“set”策略与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
}
相关问题