How can I map a field inside a document as associative array whose values are references to another document?
Let's say I have a document File
which represents some file on disk. Something like this:
/** @Document */
class File {
/** @Id */
protected $id;
/** @String */
protected $filename;
// Getter and setters omitted
}
And another document representing an image, which stores references to different sizes of an image. Something like this:
/** @Document */
class Image {
/** @Id */
protected $id;
/** ???? */
protected $files;
// Getter and setters omitted
}
I now want to be able to store some references to files in the image document keyed by their size. For exampl:
$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));
The resulting MongoDB document should look something like this:
{
"_id" : ObjectId("...."),
"files" : {
"50x50" : {
"$ref" : "files",
"$id" : ObjectId("...")
},
"100x100" : {
"$ref" : "files",
"$id" : ObjectId("...")
}
}
}
So how do I map the files
field in the Image
document?
Use "set" strategy with Doctrine's 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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.