symfony - 更新表单 vich 上传者,无法删除或编辑文件

标签 symfony doctrine-orm twig vichuploaderbundle

我无法使用 VichUploaderBundle 删除或编辑我上传的图片。我有一个带有 OneToMany(双向关系)的 Annonce 和 Photo 实体。我尝试使用属性 setUpdatedAt 来调用 vich prePersist,但他不起作用。

这是通告:

class Annonce
{
// ...
/**
 * @ORM\OneToMany(targetEntity="Immo\AnnonceBundle\Entity\Photo", mappedBy="annonce", cascade={"persist", "remove"})
 */
private $photos;

带有 setter/getterImage() 的照片实体:

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Photo
 * @Vich\Uploadable
 * @ORM\Table()
 */
class Photo
{   // id, etc. 
    /**
     * @Assert\File(
     *     maxSize="1M",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )
     * @Vich\UploadableField(mapping="uploads_image", fileNameProperty="url")
     *
     * @var File $image
     */
    protected $image;

    /**
     * @ORM\Column(type="string", length=255, name="url")
     *
     * @var string $url
     */
    protected $url;

    /**
     * @ORM\ManyToOne(targetEntity="Immo\AnnonceBundle\Entity\Annonce", inversedBy="photos")
     */
    private $annonce;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     *
     * @var \DateTime $updatedAt
     */
    protected $updatedAt;

/**
 * Set image
 *
 * @param string $image
 * @return Photo
 */
public function setImage($image)
{
    $this->image = $image;

    if ($this->image instanceof UploadedFile) {
        $this->updatedAt = new \DateTime('now');
    }

    return $this;
}

这是我的 config.yml:

knp_gaufrette:
stream_wrapper: ~
adapters:
    uploads_adapter:
        local:
            directory: %kernel.root_dir%/../web/img/uploads
filesystems:
    uploads_image_fs:
        adapter:    uploads_adapter

vich_uploader:
    db_driver: orm
    twig: true
    gaufrette: true
    storage:   vich_uploader.storage.gaufrette
    mappings:
        uploads_image:
            delete_on_remove: true
            delete_on_update: true
            inject_on_load: true
            uri_prefix:         img/uploads
            upload_destination: uploads_image_fs
            namer: vich_uploader.namer_uniqid

我的 AnnonceType:

$builder->add('photos', 'collection', array('type' => new PhotoType(),
                                                'allow_add' => true,
                                                'allow_delete' => true,
                                                'by_reference' => false,
                                                )
                  )

照片类型:

$builder->add('image', 'file')

Controller :

public function updateAnnonceAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $annonce = $em->getRepository('ImmoAnnonceBundle:Annonce')
                      ->findCompleteAnnonceById($id);

        $form = $this->createForm(new AnnonceType, $annonce);

        $request = $this->get('request');

        if ($request->getMethod() == 'POST') {
            $form->bind($request);

            if ($form->isValid()) {

                $em = $this->getDoctrine()->getManager();

                $em->persist($annonce);
                $em->flush();

                $this->get('session')->getFlashBag()->add('success', 'ok');

                return $this->redirect($this->generateUrl('immo_admin_annonce_homepage'));

            }
        }

        return $this->render('ImmoAnnonceBundle:Admin/Annonce:update.html.twig', array('annonce' => $annonce,
                                                                       'form' => $form->createView()
                                                                                      )
                            );
    }

我的模板将 Annonce 中每张照片的输入文件放入 html 中:

{{ form_widget(form.photos) }} // With JS to manage add/delete on each input.
// Return this :
<input type="file" required="required" name="immo_annoncebundle_annonce[photos][2][image]" id="immo_annoncebundle_annonce_photos_2_image">

最佳答案

在您的实体中添加“updateAt”属性 参见 http://mossco.co.uk/symfony-2/vichuploaderbundle-how-to-fix-cannot-overwrite-update-uploaded-file/了解更多信息

关于symfony - 更新表单 vich 上传者,无法删除或编辑文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22484000/

相关文章:

php - 使用学说的 Symfony 4 工作人员无法正常工作 : SQLSTATE[HY000] [2002] Connection timed out

mysql - 带子查询和的 Doctrine DQL 查询

php - 从validation.yml中读取配置设置

php - Symfony3 queryBuilder 如何使用 OR 进行搜索以及如何搜索子字符串?

php - 编译错误 : Cannot use isset() on the result of an expression

php - 如何在 Symfony2 功能测试中通过 HWIOAuth 模拟身份验证?

symfony - 学说 2 树扩展 : Closure Table

mysql - 原则 2 DQL 安全

php - 从 Angularjs 渲染 Twig 模板

php - Twig "is defined"是否适用于可迭代对象?