在 symfony4 中对 null 调用成员函数 setDp()

Call to a member function setDp() on null in symfony4

我开始学习symfony4了。我遇到的问题是使用 Symfony 表单更新数据库中的现有数据。代码中显示的成员函数设置的文件会出现问题。 有什么解决办法吗?请解决

文件移动成功,但没有设置成员函数这是代码

/**
 * @Route("/new", name="new")
 */
public function new()
{
    return $this->render('dashboard/new.html.twig', [
        'controller_name' => 'DashboardController',
    ]);
}[enter image description here][1]

/**
 * @Route("/edit/{username}", name="edit")
 */
public function edit(Request $request, $username)
{   
    $user = new User();
    $user = $this->getDoctrine()->getRepository(User::class)->find($username);
    $imageConstraints = [
        new Image([
            'maxSize' => '5M'
        ])
    ];
    $form = $this->createFormBuilder($user)
        ->add('name', TextType::class)
        ->add('username', TextType::class)
        ->add('bio', TextType::class)
        ->add('location', TextType::class)
        ->add('website', UrlType::class)
        ->add('dob', BirthdayType::class)
        ->add('dp', FileType::class, [
            'multiple' => false,
            'mapped' => false,
            'required' => false,
            'constraints' => $imageConstraints
        ])
        ->add('header_pic', FileType::class, [
            'required' => false,
            'multiple' => false,
            'mapped' => false,
            'constraints' => $imageConstraints
        ])
        ->add('save', SubmitType::class)
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $entityManager = $this->getDoctrine()->getManager();
        /**@var UploadedFile $uploadedFile */
        //dd($form['dp']->getData());

        $destination = $this->getParameter('kernel.project_dir').'/public/uploads';

        //for Dp means Profile pic
        $uploadedFileDp = $form['dp']->getData();
        $originalFilenameDp = pathinfo($uploadedFileDp->getClientOriginalName(), PATHINFO_FILENAME);
        $newFilenameDp = $originalFilenameDp.'-'.uniqid().'.'.$uploadedFileDp->guessExtension();
        $uploadedFileDp->move(
            $destination,
            $newFilenameDp
        );
        $user->setDp($newFilenameDp);

        //Header pic
        $uploadedFileHeaderPic = $form['header_pic']->getData();
        $originalFilenameHeaderPic = pathinfo($uploadedFileHeaderPic->getClientOriginalName(), PATHINFO_FILENAME); 
        $newFilenameHeaderPic = $originalFilenameHeaderPic.'-'.uniqid().'.'.$uploadedFileHeaderPic->guessExtension();

        $uploadedFileHeaderPic->move(
            $destination,
            $newFilenameHeaderPic
        );
        $user->setHeaderPic($newFilenameHeaderPic);
        $entityManager->flush();

        // do anything else you need here, like send an email

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

在 null

上调用成员函数 setDp()

您的问题在这里:

$user->setDp($newFilenameDp);

错误告诉您无法针对 null 调用 setDp,因此这意味着 $user 必须为 null 而不是用户的实例您期待的对象。

你有这行代码:

$user = $this->getDoctrine()->getRepository(User::class)->find($username);

您的 find 可能会失败并返回空值,即您无法真正找到用户并用您的对象填充 $user。此时您应该在编辑方法中抛出异常或中止。


$user = $this->getDoctrine()->getRepository(User::class)->find($username);
if( $user === null or $user instanceof WhatObjectYouExpect === false )
{
   throw new Exception('Failed to load user data');
}

我找到了解决方案,就是在路由中传递主键 $id 而不是 $username ,这样就不会给出空值。这是传递用户 ID 以更新 post.

的树枝超链接

<a href="edit/{{app.user.id}}">Edit</a>