简体   繁体   English

Doctrine ODM:嵌入多个 GridFS 文档异常

[英]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.我正在尝试通过 Doctrine / Symfony 2 在 GridFS 中的主要较大图像中嵌入缩略图。

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 ,当我尝试持久化/刷新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?或者有没有更好的方法来将这些图像及其关系存储在 mongo 中?

Field, which maps GridFS have to be the top-most-level document.映射 GridFS 的字段必须是最顶层的文档。 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.这是因为 GridFS 在 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更多详情: https : //github.com/doctrine/mongodb-odm/issues/911

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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