[英]Call to a member function setDp() on null in symfony4
I am beginning to learn symfony4. 我开始学习symfony4。 I am stuck with the problem that is to update the existing data in the database by using Symfony form.
我遇到的问题是使用Symfony表单更新数据库中的现有数据。 The problem occurs with the file to set by the member function which is shown in the code.
代码中显示的成员函数要设置的文件会出现问题。 Have any solution?
有什么解决办法吗? please resolve
请解决
The file move successfully but no set with the member function Here is the code 文件移动成功,但未使用成员函数进行设置这是代码
/**
* @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())
);
}
}
Call to a member function setDp()
on null 在null上调用成员函数
setDp()
Your problem is here: 您的问题在这里:
$user->setDp($newFilenameDp);
The error is telling you that the setDp
can't be called against null
, therefore what that then means is $user
must be null and not an instance of the user object you are expecting. 该错误告诉您不能针对
null
调用setDp
,因此,这意味着$user
必须为null,而不是您期望的用户对象的实例。
You have this line of code: 您具有以下代码行:
$user = $this->getDoctrine()->getRepository(User::class)->find($username);
It is possible that your find
is failing and returning you the null, ie you're failing to actually locate the user and populate $user
with your object. 您的
find
可能失败并返回空值,即您未能真正找到用户并向$user
填充对象。 You should be throwing an exception in your edit method or aborting at this point. 此时,您应该在编辑方法中引发异常或中止操作。
$user = $this->getDoctrine()->getRepository(User::class)->find($username);
if( $user === null or $user instanceof WhatObjectYouExpect === false )
{
throw new Exception('Failed to load user data');
}
I found the solution, which is to pass the primary key $id instead of $username in the route which will not give the null value. 我找到了解决方案,即在不会给出空值的路由中传递主键$ id而不是$ username。 Here is the twig hyperlink to pass the user id to update the post.
这是细枝超链接,用于传递用户ID来更新帖子。
<a href="edit/{{app.user.id}}">Edit</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.