我有一个与图片(图片模型)具有多对多关系的对象“内容”。一切正常,直到我坚持(图片已上传,移动到适当的文件夹,工作正常)。persist 命令返回以下错误:
在关联 Creator\MainBundle\Entity\Content#pictures 上找到 Symfony\Component\HttpFoundation\File\UploadedFile 类型的实体,但期待 Creator\MainBundle\Entity\Picture
我理解错误的含义,它期望的是图片对象而不是上传的文件对象。我不明白从这里去哪里,我读过的所有文档都只显示了如何将单个文件上传到单个列。
图片模型只有一个字段:file(字符串)。
任何帮助将非常感激。
内容控制器代码:
$form = $this->createFormBuilder($content, array('validation_groups'=>array('upload_'.$type)))
->add('picture')
->add('Upload '.ucfirst($type), 'submit')
->getForm();
$form->handleRequest($request);
if($request->getMethod()=='POST' && $form->isvalid())
{
$content->uploadPicture();
$em->persist($content);
$em->flush();
$session = $request->getSession();
$session->getFlashBag()->add('success', 'Upload successful');
}
内容模型代码:
public function addPictures($picture)
{
$this->pictures[] = $picture;
return $this;
}
public function getPicture()
{
return $this->picture;
}
public function setPicture($picture)
{
$this->picture = $picture;
return $this;
}
public function getPictures()
{
return $this->pictures;
}
public function setPictures($pictures)
{
$this->pictures = $pictures;
return $this;
}
public function uploadPicture()
{
if (null === $this->getPicture()) {
return;
}
$extension = $this->getPicture()->getExtension();
if(!$extension) $extension = 'jpg';
// unique file name
$fname = uniqid().'.'.$extension;
$this->getPicture()->move(
$this->getUploadRootDir(),
$fname
);
$this->makeThumbnail($fname, $extension);
$this->addPictures($this->getPicture());
}