Image VichUploader:映像更新不工作
我在我的主页横幅上有四张图片,我希望能够时不时地使用Symfony表单更新它们 我完全按照Vich Uploader文档中的描述设置了一个图像实体(在实体上更新了以及所有内容) 我的数据库表中已经有一些图片,因此我可以访问控制器操作的路径,呈现的表单仅包含以下内容:Image VichUploader:映像更新不工作,image,symfony,vichuploaderbundle,Image,Symfony,Vichuploaderbundle,我在我的主页横幅上有四张图片,我希望能够时不时地使用Symfony表单更新它们 我完全按照Vich Uploader文档中的描述设置了一个图像实体(在实体上更新了以及所有内容) 我的数据库表中已经有一些图片,因此我可以访问控制器操作的路径,呈现的表单仅包含以下内容: public function buildForm(FormBuilderInterface $builder, array $options): void { $builder->add('imageFile'
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('imageFile', VichImageType::class, [
'required' => false,
'allow_delete' => false,
'download_label' => '...',
'download_uri' => true,
'image_uri' => true,
]);
}
正如您所看到的,我对基本文档的修改还不多。
无论如何,当使用ID7进入该路线时,图片7显示在下载uri下,当转储imageName或UpdatedAt时,其数据与我的数据库表中显示的数据相同。但是,当我尝试上传一张新图片以取代旧图片时,我得到一个错误,即
实体AppBundle\Entity\Image@0000000075aa1d6c00000000789d890b不受管理。如果实体从数据库中获取或通过EntityManager注册为新实体,则该实体将被管理
或者有时它像奇迹一样通过,我被重定向,但我的数据库表中没有任何更改,所以旧图片没有被替换或其他任何东西
我搜索了很多,找到了很多关于未管理的实体的解决方案,或者没有更新,因为没有更改属性。但他们都没有解决我的问题
你在我的代码中看到任何可能导致问题的东西吗
更新
我将@nifr的setImageFile方法添加到我的图像实体中:
/**
* @param File|UploadedFile|null $imageFile
*
* @return $this
*/
public function setImageFile(File $imageFile = null)
{
$this->imageFile = $imageFile;
dump($imageFile);
if ($imageFile instanceof UploadedFile) {
$this->setFileSize($imageFile->getClientSize());
$this->setMimeType($imageFile->getClientMimeType());
$this->setOriginalName($imageFile->getClientOriginalName());
$this->updatedAt = new \DateTime();
}
return $this;
}
当在if条件中转储时,它什么也不做,因此显然它不将$imageFile识别为UploadedFile的实例。但是,在此之前转储时(如复制的代码中),会转储以下信息:
UploadedFile {#14 ▼
-test: false
-originalName: "Route.png"
-mimeType: "image/png"
-size: 13218
-error: 0
path: "/tmp"
filename: "phpQhWrTd"
basename: "phpQhWrTd"
pathname: "/tmp/phpQhWrTd"
extension: ""
realPath: "/tmp/phpQhWrTd"
aTime: 2018-02-05 19:03:12
mTime: 2018-02-05 19:03:12
cTime: 2018-02-05 19:03:12
inode: 311536
size: 13218
perms: 0100600
owner: 1001
group: 33
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
}
所以很明显,它来自“上传文件”。有什么想法吗
我尝试的也是在我的控制器中转储:
public function uploadAction(Request $request, $id){
$repository = $this->getDoctrine()->getRepository('AppBundle:Image');
$em = $this->getDoctrine ()->getManager ();
$image = $repository->findOneById($id);
dump($image);
$form = $this->createForm(UploadImageType::class, $image);
$form->handleRequest($request);
if ($form->isValid() && $form->isValid() ) {
dump($image->getImageName());
$em->flush();
return $this->redirectToRoute('index');
}
return $this->render('AppBundle:Image:upload.html.twig', array(
'form' => $form->createView(),
'image' => $image,
));
}
在图像文件设置器中有转储文件时我意识到,我在控制器中的转储文件包含“旧”图像名称,例如名称A,但设置器包含“新”图像名称(来自应替换旧图像的图像),因此实际上是正确的图像名称。但它永远不会更新。这并不是您遇到的非托管实体错误的答案 为了确保图像得到正确更新,您需要在实体中使用如下方法:
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/** @var \DateTime|null */
protected $updatedAt;
/**
* @param File|UploadedFile|null $file
*
* @return $this
*/
public function setFile(File $file = null)
{
$this->file = $file;
if ($file instanceof UploadedFile) {
$this->setFileSize($file->getClientSize());
$this->setMimeType($file->getClientMimeType());
$this->setOriginalName($file->getClientOriginalName());
$this->updatedAt = new \DateTime();
}
return $this;
}
您的图像的模式结构是什么,例如
descripe images
等。我在您的问题中添加了一些代码,这些代码虽然不能直接回答您的问题,但可能会帮助其他用户在将来通过vich uploader bundle
更新文件时遇到问题。你解决了你的问题吗?@sakhunzai抱歉,我想我没有100%地理解你的问题,你能试着再问一次吗,换一种方式吗?@nifr不,我还没有解决我的问题,我仍然很乐意得到帮助!我在查看数据库表中是否有updatedAt
列,正如nifr回答的那样,belowI的想法是相同的updatedAt
这应该是答案,但是,我在实体中有一个setImageFile方法,其中定义了$updatedAt get。当使用instanceOf时,它不起作用..当使用您的方法时,它会提交,但我的数据库中没有任何更改。所以setFile什么都没做。。检查UploadedFile的instanceOf不会导致True是否确实调用了setFile
。@nifr会调用它,但当例如在“instanceOf”检查后放置转储时,它不会转储,因此不会进入该if状态。。在$file进入if条件之前转储它时,我得到了信息,我只是在我的问题中添加了信息
public function uploadAction(Request $request, $id){
$repository = $this->getDoctrine()->getRepository('AppBundle:Image');
$em = $this->getDoctrine ()->getManager ();
$image = $repository->findOneById($id);
dump($image);
$form = $this->createForm(UploadImageType::class, $image);
$form->handleRequest($request);
if ($form->isValid() && $form->isValid() ) {
dump($image->getImageName());
$em->flush();
return $this->redirectToRoute('index');
}
return $this->render('AppBundle:Image:upload.html.twig', array(
'form' => $form->createView(),
'image' => $image,
));
}
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/** @var \DateTime|null */
protected $updatedAt;
/**
* @param File|UploadedFile|null $file
*
* @return $this
*/
public function setFile(File $file = null)
{
$this->file = $file;
if ($file instanceof UploadedFile) {
$this->setFileSize($file->getClientSize());
$this->setMimeType($file->getClientMimeType());
$this->setOriginalName($file->getClientOriginalName());
$this->updatedAt = new \DateTime();
}
return $this;
}