Symfony FatalThrowableError:在null上调用成员函数getId()

时间:2019-10-04 06:18:14

标签: php symfony symfony4 sylius

我正在做一个电子商务网站,最后我发现修改客户信息的信息时出现了一个错误:

错误如下:

Uncaught PHP Exception Error: "Call to a member function getId() on null" 
at C:\wamp64\www\caviar-pro\src\Controller\CustomerController.php line 339

这是有问题的代码部分: enter image description here

完整的代码如下:

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);
}

如果有人可以告诉我解决问题的方法,我很感兴趣,因为我在网站的这一部分停留了一段时间。

1 个答案:

答案 0 :(得分:1)

!$defaultAddress->getId(),id将一直存在,除非该实体尚未持久或为null。  根据您的情况,$defaultAddress为空。 (转储变量以查看)。

我认为,您应该执行以下操作:

if (!$defaultAddress) {
    $defaultAddress = new AddressObject(); //change this to your address class
    $defaultAddress->setFirstName($resource->getFirstName());
    $defaultAddress->setLastName($resource->getLastName());
}