我在Symfony中尝试编辑记录。我可以编辑文本记录但是我无法在记录中编辑新文件。((文件保存在存储中但不保存在数据库上))
我如何使用VichUploaderBundle 1.4在Symfony 3.4中做到这一点...... 对于插入,我没有任何问题我将图像上传到存储并保存在数据库中 .....
这是我的editAction
/**
* @Route("/Article/editArticle/{id}",name="editArticle")
*/
public function editArticleAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$articleRepo=$em->getRepository('AdminBundle:Article');
$articleata = $articleRepo->find($id);
// $satelliteImage=new satelliteImage;
$article = new Article();
$form = $this->createForm(ArticleType::class,$article,array(
'action' => $this->generateUrl('editArticle',array('id' => $articleata->getId())),
'attr' => array(
'class' => 'dropzone',
'id' => "my-awesome-dropzone"
),
'method' => 'POST',
[
'name' => $articleata->getName(),
'title' => $articleata->getTitle(),
'subject' => $articleata->getSubject(),
'description' => $articleata->getDescription(),
'smallPic' => $articleata->getSmallPic(),
'largPic' => $articleata->getLargPic(),
'displayStatus' => $articleata->getDisplayStatus()]
));
//
if ($request->getMethod() == Request::METHOD_POST){
$form->handleRequest($request);
$article->setName($form->get('name')->getData());
$article->setTitle($form->get('title')->getData());
$article->setSubject($form->get('subject')->getData());
$article->setDescription($form->get('description')->getData());
$article->setDisplayStatus($form->get('displayStatus')->getData());
$article->setSmallPic($form->get('imageFile')->getData());
$article->setLargPic($form->get('imageFile2')->getData());
$em = $this->getDoctrine()->getManager();
$em->flush();
}
return $this->render('AdminBundle:Article:edit_article.html.twig', array(
'form' => $form->createView(),
'Articles' => $articleata
));
}
我的实体
private $smallPic;
/**
* @Vich\UploadableField(mapping="articles_images", fileNameProperty="smallPic")
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="datetime")
* @var \DateTime
*/
private $updatedAt;
/**
* @var string
*
* @ORM\Column(name="largPic", type="string", length=255)
*/
private $largPic;
/**
* @Vich\UploadableField(mapping="articles_images", fileNameProperty="largPic")
* @var File
*/
private $imageFile2;
public function setImageFile(File $smallPic = null)
{
$this->imageFile = $smallPic;
// VERY IMPORTANT:
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($smallPic) {
// if 'updatedAt' is not defined in your entity, use another property
$this->updatedAt = new \DateTime('now');
}
}
public function getImageFile()
{
return $this->imageFile;
}
public function setSmallPic($smallPic)
{
$this->smallPic = $smallPic;
}
public function getSmallPic()
{
return $this->smallPic;
}
public function setImageFile2(File $largPic = null)
{
$this->imageFile2 = $largPic;
// VERY IMPORTANT:
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($largPic) {
// if 'updatedAt' is not defined in your entity, use another property
$this->updatedAt = new \DateTime('now');
}
}
public function getImageFile2()
{
return $this->imageFile2;
}
/**
* Set largPic
*
* @param string $largPic
*
* @return Article
*/
public function setLargPic($largPic)
{
$this->largPic = $largPic;
}
/**
* Get largPic
*
* @return string
*/
public function getLargPic()
{
return $this->largPic;
}
.
.
.
FromType
->add('imageFile', VichFileType::class, array(
'label' => "smallpic",
'required' => false,
'allow_delete' => true, // not mandatory, default is true
'download_link' => true, // not mandatory, default is true
))
->add('imageFile2', VichFileType::class, array(
'label' => "largPic",
'required' => false,
'allow_delete' => true, // not mandatory, default is true
'download_link' => true, // not mandatory, default is true
))
config.yml
vich_uploader:
db_driver: orm
mappings:
articles_images:
uri_prefix: '%app.path.articles_images%'
upload_destination: '%kernel.root_dir%/../web/uploads/images/articles'
inject_on_load: true
delete_on_update: true
delete_on_remove: true
答案 0 :(得分:1)
在冲洗之前,你需要:
$em->persist($article);
更新:
通常,VichUploader Bundle不再被视为上传图像的最佳做法。在DB中存储图像数据确实会增加它的大小,并且不建议这样做。你有另一个名为media的实体,有一些字段,如hash
,path
,size
等...你应该使用文件系统,如果你想改变,可以使用抽象,例如,未来的亚马逊S3或谷歌存储。
查看使用flysystem bundle的令人敬畏的flysystem library。