简体   繁体   中英

Doctrine ODM: Embedding multiple GridFS documents exception

I'm trying to embed a thumbnail image inside of it's main larger image in GridFS via Doctrine / Symfony 2.

The main image document is as follows,

<?php 

namespace namespace\goes\here\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class FullSizeImage
{

    /** @MongoDB\Id */
    private $id;

    /** @MongoDB\File */
    private $file;

    /** @MongoDB\String */
    private $filename;

    /** @MongoDB\String */
    private $mimeType;

    /** @MongoDB\Date */
    private $uploadDate;

    /** @MongoDB\Int */
    private $userId;

    /** @MongoDB\Int */
    private $length;

    /** @MongoDB\Int */
    private $chunkSize;

    /** @MongoDB\String */
    private $md5;

    /** @MongoDB\Collection */
    private $tags = array();

    /** @MongoDB\EmbedOne(targetDocument="Thumbnail") */
    private $thumbnail;

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getTags()
    {
        return $this->tags;
    }

    public function setTags($tags)
    {
        $this->tags = $tags;
    }

    public function getFile()
    {
        return $this->file;
    }

    public function setFile($file)
    {
        $this->file = $file;
    }

    public function getFilename()
    {
        return $this->filename;
    }

    public function setFilename($filename)
    {
        $this->filename = $filename;
    }

    public function getMimeType()
    {
        return $this->mimeType;
    }

    public function setMimeType($mimeType)
    {
        $this->mimeType = $mimeType;
    }

    public function getChunkSize()
    {
        return $this->chunkSize;
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getMd5()
    {
        return $this->md5;
    }

    public function getUploadDate()
    {
        return $this->uploadDate;
    }

    public function getUserId()
    {
        return $this->userId;
    }

    public function setUserId($userId)
    {
        $this->userId = $userId;
    }

    public function getThumbnail()
    {
        return $this->thumbnail;
    }

    public function setThumbnail($thumbnail)
    {
        $this->thumbnail = $thumbnail;
    }

}

and the thumbnail document, that lives under the same namespace, is as follows,

<?php

namespace namespace\goes\here\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/** @MongoDB\EmbeddedDocument */
class Thumbnail
{

    /** @MongoDB\Id */
    private $id;

    /** @MongoDB\File */
    private $file;

    /** @MongoDB\String */
    private $filename;

    /** @MongoDB\String */
    private $mimeType;

    /** @MongoDB\Date */
    private $uploadDate;

    /** @MongoDB\Int */
    private $length;

    /** @MongoDB\Int */
    private $chunkSize;

    /** @MongoDB\String */
    private $md5;

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getFile()
    {
        return $this->file;
    }

    public function setFile($file)
    {
        $this->file = $file;
    }

    public function getFilename()
    {
        return $this->filename;
    }

    public function setFilename($filename)
    {
        $this->filename = $filename;
    }

    public function getMimeType()
    {
        return $this->mimeType;
    }

    public function setMimeType($mimeType)
    {
        $this->mimeType = $mimeType;
    }

    public function getChunkSize()
    {
        return $this->chunkSize;
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getMd5()
    {
        return $this->md5;
    }

    public function getUploadDate()
    {
        return $this->uploadDate;
    }

}

When I try to persist / flush the FullSizeImage ,

$thumbnail = new Thumbnail();
$thumbnail->setFile($thumbPath);
$thumbnail->setFilename($thumbName);
$thumbnail->setMimeType($thumbMimeType);
$thumbnail->setUserId($user->getId());

$fsi = new FullSizeImage();
$fsi->setFile($file->getRealPath());
$fsi->setTags(array('tag', 'tag', 'tag'));
$fsi->setFilename($file->getFilename());
$fsi->setMimeType($file->getMimeType());
$fsi->setUserId($user->getId());
$fsi->setThumbnail($thumbnail);

$dm->persist($fsi);
$dm->flush();

I encounter this exception,

Could not store file: zero-length keys are not allowed, did you use $ with double quotes?

Am I missing something? Or is there a better way to go about storing these images and their relationship in mongo?

Field, which maps GridFS have to be the top-most-level document. It can be referenced by other documents, but it can't be embed by any other document. It's because GridFS implementation in Mongo ODM.

I've had a similiar issue some time ago. I've ended up with solution described above.

More details: https://github.com/doctrine/mongodb-odm/issues/911

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM