无法更新实体包含图像字段

时间:2017-07-17 12:44:43

标签: php mysql symfony doctrine

我有一个事件实体,

- 管理员创建事件并可以更新所有字段

- 用户可以只更新一些字段(在我的情况下,我有一个图像字段无法由用户更新)

我的问题是当用户更新事件时我出错了!我该如何解决这个问题?

错误:

The file could not be found. 
    Caused by: 
               ConstraintViolation {#601 ▼
               root: Form {#788 ▶}
               path: "data.image"
               value: "C:\wamp64\tmp\php4F0E.tmp"
            }

活动实体

class Event implements AdvancedUserInterface
{
/**
 * @var string
 *
 *    @Assert\Image(
 *     minWidth = 60,
 *     maxWidth = 400,
 *     minHeight = 60,
 * )
 *
 * @ORM\Column(name="image", type="string", length=255, nullable=true)
 */
private $image;
}

我没有放置图片字段,因为用户没有更新权限

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('eventName')
            ->add('eventAdresse')
            ->add('eventAdresse')
            ->add('startEventDate', DateType::class, array(
         'widget' => 'single_text',     
         'format' => 'yyyy-MM-dd'))
           ->add('endEventDate', DateType::class, array(
            'widget' => 'single_text',
        'format' => 'yyyy-MM-dd'))

}

控制器

class EventController extends Controller
{
    public function indexAction(Request $request)
    {
        $event = $this->getUser();

        $form = $this->createForm('AppBundle\Form\EventType',$event);

        $form ->handleRequest($request);
        if($form->isSubmitted() && $form->isValid())
        {
            $em = $this->getDoctrine()->getManager();


            $em->flush();
            return $this->redirectToRoute('event_homepage');
        }

        return $this->render('AppBundle:Event:index.html.twig', array(
            'event' => $event,
            'form' => $form->createView(),
        ));
    }
}

更新 这是我第一次在管理员添加我遵循此文档的事件时的代码 https://symfony.com/doc/current/controller/upload_file.html

在数据库中,路径持久化只是文件的名称:1cd665f6d16f13d7b293616dcee077d2.png?

public function addAction(Request $request)
{
    $event=new Event();
    $form = $this->createForm('AppBundle\Form\EventType,$event);

    $form ->handleRequest($request);
    if($form->isSubmitted() && $form->isValid())
    {
        $factory = $this->get('security.encoder_factory');
        $encoder = $factory->getEncoder($event);
        $password = $encoder->encodePassword($event->getPassword(), $event
->getSalt());
        $event->setPassword($password);




        if($event ->getImage()){
            $file = $event->getImage();

            // Generate a unique name for the file before saving it
            $fileName = md5(uniqid()).'.'.$file->guessExtension();

            // Move the file to the directory where brochures are stored
            $file->move(
                $this->getParameter('images_directory_event'),
                $fileName
            );
            $event ->setImage($fileName);
        }



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


        $em->flush();
        return $this->redirectToRoute('administrator_event');
    }

    return $this->render('AppBundle:Event:add.html.twig', array(
        'form' => $form->createView(),
    ));


}

IN parameters.yml

images_directory_event: '%kernel.root_dir%/../web/uploads/imagesEvent'

1 个答案:

答案 0 :(得分:1)

您在上传时保存文件的位置,这是一个临时文件,不会永久保留。您必须手动将文件移动到所需的位置,然后相应地更新您的实体。您收到验证错误,因为即使您没有编辑该字段,您仍然在对其执行Symfony验证,并且该文件不再存在。

请查看Symfony documentation for file uploads以获取更多帮助。当第一次持久化实体时,你会想要做这样的事情:

public function newAction(Request $request)
{
    $form = $this->createForm('AppBundle\Form\EventType', new Event());
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $file = $event->getImage();
        $fileName = 'the-file-name-you-choose.'.$file->guessExtension();

        $file->move('/the/upload/directory', $fileName);

        $event->setImage($fileName);

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

        return $this->redirect($this->generateUrl('wherever_you_want'));
    }

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