2013-01-25 26 views
1

如何將文檔內的字段映射爲值爲另一個文檔的引用的關聯數組?在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文檔中?

回答

0

使用 「套」 戰略,學說的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 
} 
相關問題