使用Symfony编辑实体中的文件名 [英] Edit file name in entity with Symfony

查看:104
本文介绍了使用Symfony编辑实体中的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好(对不起,我的英语不太好)

我实际上是在一个展示一些眼镜的Symfony网站上工作的信息。
现在,我需要在创建其中一个图像时添加图像。我设法借助 教程。

I'm actually working on a Symfony website that display some spectacles' information. For now, I need to add an image when I create one of them. I managed to do so with the help of this tutorial.

基本上是这样的:我将图片上传到网站目录中,然后将文件名发送给实体(存储在MySQL数据库中)。然后,我可以在眼镜的详细信息中显示图像。

It basically works like this: I upload an image into site's directory and then send file's name to the entity (Store in a MySQL database). I can then display the image in spectacle's details.

我要编辑眼镜时出现了问题。我无法更新图片的名称。我只有两种可能性:1 /不编辑实体,或者2 /更改图像名称,然后得到一个我不再显示的随机图像(这些名称通常像 / tmp / phpWb8kwV

The issue appeared when I want to edit a spectacle. I can't update the image's name. I have two only possibilities are to 1/not edit the entity, or to 2/change the image name and then get a random one that I can't display anymore (these names are usually like /tmp/phpWb8kwV)

我的图像在实体(在Spectacle.php中)这样的实例化:

/**
* @var string
*
* @ORM\Column(name="image", type="string", length=255)
* @Assert\NotBlank(message="Veuillez ajouter une image à votre spectacle.")
* @Assert\File(mimeTypes={ "image/png" })
*/
private $image;

然后眼镜形式的FormType就是这样(在SpectacleType.php中)

And the FormType for the spectacle's form is made like this (in SpectacleType.php):

 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('nom')
            ->add('lieu')
            ->add('dateSpectacle', null, array(
                'label' => 'Date du spectacle',
            ))
            ->add('annee')
            ->add('image',FileType::class, array(
                'label' => 'Image du spectacle',
                'required' => false, //(Still need to provide a file to finalize the creation/edit)
            ));
}

访问此页面的控制器是这样的(in SpectacleController.php)

And the controller to acces this page is made like this (in SpectacleController.php):

/**
 * Creates a new spectacle entity.
 *
 * @Route("/new", name="admin_spectacle_new")
 * @Method({"GET", "POST"})
 */
public function newAction(Request $request)
{
    $spectacle = new Spectacle();
    $form = $this->createForm('FabopBundle\Form\SpectacleType', $spectacle);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
//--------------------------------------------------------------------
        $file = $spectacle->getImage();            
        $fileName = (md5(uniqid())).'.'.$file->guessExtension();            
        // moves the file to the directory where image are stored
        $file->move(
            $this->getParameter('img_directory'), //(Define in the service.yml)
            $fileName
        );
        $spectacle->setImage($fileName); //(Don't know how to handle file names without this line)
//---------------------------------------------------------------------
        $em->persist($spectacle);
        $em->flush();
        return $this->redirectToRoute('admin_spectacle_show', array('id' => $spectacle->getId()));
    }

    return $this->render('spectacle/new.html.twig', array(
        'spectacle' => $spectacle,
        'form' => $form->createView(),
    ));
}

路由到编辑视图的函数大致相同,但是我不能使用

The function that is routed to the edit view is approximatively the same, but i can't use

$spectacle->setImage($fileName);






有两种方法可以解决此问题:例如能够更新实体中的新文件名(以及其他信息),或者能够在不更改文件名的情况下更新实体。


There is two possibilities to solve this: I would like being able to update the new filename in the entity (with the other information) or being able to update the entity without changing filename.

我希望我足够清楚解释我的问题...
预先感谢您的答复。

I hope I was clear enough to explain my problem... Thanks in advance for your responses.

推荐答案

解决方案很愚蠢...

The solution was stupid ...

实际上,用于访问 edit 路线的控制器没有以下行:

In fact, the controller to access the edit route didn't have those lines:

$em = $this->getDoctrine()->getManager();
...
$em->persist($spectacle);
$em->flush();




我需要快速完成此操作。如果我以后有更多时间,我将尝试使其与ComurImageBundle一起使用。

I need to finish this quickly. If i have more time later, i'll try to make it work with the ComurImageBundle.

感谢您的帮助,我下次会更小心...

Thank you for your help, i'll be more carefull next time ...

这篇关于使用Symfony编辑实体中的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆