I am doing an e-commerce site, and in the end I realized a bug when modifying the informations of the customers informations:
the error is as follows:
Uncaught PHP Exception Error: "Call to a member function getId() on null"
at C:\wamp64\www\caviar-pro\src\Controller\CustomerController.php line 339
Here is the part of the code that is problematic:
The complete code is the following:
public function updateAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, ResourceActions::UPDATE);
$resource = $this->findOr404($configuration);
$form = $this->resourceFormFactory->create($configuration, $resource);
$oldInfo = clone $resource;
$oldAddressInfo = $oldInfo->getDefaultAddress() ? clone $oldInfo->getDefaultAddress() : null;
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && $form->handleRequest($request)->isValid()) {
/** @var CustomerInterface $resource */
$resource = $form->getData();
$defaultAddress = $resource->getDefaultAddress();
if (!$defaultAddress->getId()) {
$defaultAddress->setFirstName($resource->getFirstName());
$defaultAddress->setLastName($resource->getLastName());
}
/** @var ResourceControllerEvent $event */
$event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource);
if ($event->isStopped() && !$configuration->isHtmlRequest()) {
throw new HttpException($event->getErrorCode(), $event->getMessage());
}
if ($event->isStopped()) {
$this->flashHelper->addFlashFromEvent($configuration, $event);
if ($event->hasResponse()) {
return $event->getResponse();
}
return $this->redirectHandler->redirectToResource($configuration, $resource);
}
try {
$this->resourceUpdateHandler->handle($resource, $configuration, $this->manager);
$valueChangedPersonnalInfo = $this->compareCustomer($oldInfo, $resource);
$valueChangedAddress = $oldAddressInfo ? $this->compareAddressCustomer($oldAddressInfo, $resource->getDefaultAddress()) : [];
$valueChanged = array_merge($valueChangedPersonnalInfo, $valueChangedAddress);
/*sending email according to template bo*/
/** @var Email $email */
$email = $this->manager->getRepository('App\Entity\Email\Email')->findOneByCode(MailerManager::ACCOUNT_MODIFICATION_MAILER_CODE);
$user = $this->getUser();
$emailManager = $this->emailManager->letterParagraphBy($email->getContent(), $email, $user->getCustomer());
$emailToSend = $this->renderView('@SyliusShop/Email/userInfoChange.html.twig', [
'infoChanged' => $valueChanged,
'email' => $email,
'emailManager' => $emailManager,
]);
$emailFrom = [$email->getFromEmail() => $email->getFromName()];
$this->mailerManager->sendMail($resource->getEmail(), $email->getSubject(), $emailToSend, $emailFrom);
} catch (UpdateHandlingException $exception) {
if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle(
$configuration,
View::create($form, $exception->getApiResponseCode())
);
}
$this->flashHelper->addErrorFlash($configuration, $exception->getFlash());
return $this->redirectHandler->redirectToReferer($configuration);
}
$postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);
if (!$configuration->isHtmlRequest()) {
$view = $configuration->getParameters()->get('return_content', false) ? View::create($resource, Response::HTTP_OK) : View::create(null, Response::HTTP_NO_CONTENT);
return $this->viewHandler->handle($configuration, $view);
}
$this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource);
if ($postEvent->hasResponse()) {
return $postEvent->getResponse();
}
return $this->redirectHandler->redirectToResource($configuration, $resource);
}
if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
}
$initializeEvent = $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource);
if ($initializeEvent->hasResponse()) {
return $initializeEvent->getResponse();
}
$carrousels = $this->manager->getRepository(Carrousel::class)->findOneBy(['code' => 'my_account_carrousel', 'enabled' => true]);
$view = View::create()
->setData([
'configuration' => $configuration,
'metadata' => $this->metadata,
'resource' => $resource,
$this->metadata->getName() => $resource,
'form' => $form->createView(),
'carrousels' => $carrousels,
])
->setTemplate($configuration->getTemplate(ResourceActions::UPDATE . '.html'))
;
return $this->viewHandler->handle($configuration, $view);
}
If someone could tell me the solution to my problem I'm interested because I'm stuck on this part of my site for a while.
By !$defaultAddress->getId()
, id will always exists except the entity is not yet persisted or null.
On you case, $defaultAddress
is null. (dump the variable to see).
I think, you should do something like:
if (!$defaultAddress) {
$defaultAddress = new AddressObject(); //change this to your address class
$defaultAddress->setFirstName($resource->getFirstName());
$defaultAddress->setLastName($resource->getLastName());
}