将Doctrine 2 ODM文档保存到Mongo(GridFS)时出错

时间:2014-05-30 14:24:46

标签: mongodb symfony doctrine-orm gridfs doctrine-odm

我试图通过Symfony2中的Doctrine ODM将文档保存到MongoDB(GridFS),但是当我尝试保留文档时,我收到此错误,

PHP Catchable fatal error:  Argument 1 passed to Doctrine\MongoDB\GridFSFile::setMongoGridFSFile() must be an instance of MongoGridFSFile, null given, called in /path/to/project/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/GridFS.php on line 97 and defined in /path/to/project/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/GridFSFile.php on line 154

奇怪的是,当我去检查MongoDB时,文件就在那里,我可以下载它。

我已检查过该文件的权限,但问题并非如此。

实体:

namespace Path\Here\SomeBundle\Document;

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


/**
 * @ODM\Document(db="files_and_stuff",collection="fs",slaveOkay=true)
 */

class UploadFile
{
    /**
     * @ODM\Id
     */
    private $id;

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

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

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

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

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

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

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

    public function getId()
    {
        return $this->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;
    }

}

经理方法:

public function uploadDocument() {

    $fileName = uniqid();

    $tmpPath = "/tmp/" . $fileName;
    imagepng($image, $tmpPath, 0);

    $UploadFile = new UploadFile();
    $UploadFile->setFilename($fileName);
    $UploadFile->setMimeType('image/png');
    $UploadFile->setFile($tmpPath);

    $this->dm->persist($UploadFile);
    $this->dm->flush();

    unlink($tmpPath);

    return $UploadFile;

}

0 个答案:

没有答案