我有一个用户文档,该文档需要使用 GridFS 为该文档提供多张图片。我无法使用 GridFS 保存文件。
这是我的用户文档:
namespace Main\UserBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
*@MongoDB\Document(db="mydb", collection="users")
*/
class User
{
/**
* @MongoDB\Id
*/
protected $id;
/** @MongoDB\EmbedMany(targetDocument="Asset") */
public $assets = array();
public function __construct()
{
$this->assets = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Add assets
*
* @param Main\ClassifiedBundle\Document\Asset $assets
*/
public function addAsset(\Main\ClassifiedBundle\Document\Asset $asset)
{
$this->assets[] = $asset;
}
/**
* Get assets
*
* @return Doctrine\Common\Collections\Collection $assets
*/
public function getAsset()
{
return $this->assets;
}
}
这是我的资产文件:
namespace Main\UserBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/** @MongoDB\EmbeddedDocument */
class Asset
{
/**
* @MongoDB\Id
*/
public $id;
/** @MongoDB\Field */
public $name;
/** @MongoDB\File */
public $file;
/** @MongoDB\Field */
public $uploadDate;
/** @MongoDB\Field */
public $length;
/** @MongoDB\Field */
public $chunkSize;
/** @MongoDB\Field */
public $md5;
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function getFile()
{
return $this->file;
}
public function setFile($file)
{
$this->file = $file;
}
}
现在在我的控制器中,我正在尝试执行以下操作:
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$user = $dm->getRepository('MainUserBundle:User')
->find(1); //I already have a user document with id 1
$asset = new Asset();
$asset->setName('Test image');
$asset->setFile($uploadedFile->getPathname()); //I have verified that this is a valid image
$user->addAsset($asset);
var_dump($user);
$dm->persist($user);
$dm->flush();
我在日志中看到的错误是:
MongoException: zero-length keys are not allowed, did you use $ with double quotes? (uncaught exception)
我在以下位置得到它:
myapp.com/vendor/doctrine-mongodb/lib/Doctrine/MongoDB/Collection.php line 203
文档也没有保存。我错过了什么?我已按照 mongoodm 网站上的所有步骤进行操作。??