首页 > 解决方案 > Symfony 控制器相同的功能不起作用

问题描述

我有 2 个控制器,广告和个人资料。我看到了 2 个导航 - 我的广告和我的个人资料。

在广告控制器动作是:

 /**
 * @Route("/my/adverts", name="my_advert_index")
 *
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function indexAction()
{
    $this->denyAccessUnlessGranted("ROLE_USER");

    $entityManager = $this->getDoctrine()->getManager();
    $adverts = $entityManager->getRepository(Advert::class)->findBy(["owner" => $this->getUser()]);

    return $this->render("MyAdvert/index.html.twig", ["adverts" => $adverts]);
}

在配置文件控制器中:

/**
 * @Route("my/profile/details", name="my_profile_details")
 *
 * @param Request $request
 * @return Response
 */
public function detailsAction(Request $request)
{
    $this->denyAccessUnlessGranted("ROLE_USER");

    $entityManager = $this->getDoctrine()->getManager();
    $profile = $entityManager->getRepository(Profile::class)->findBy(["user" => $this->getUser()]);

    return $this->render("MyProfile/details.html.twig", [
        "profile" => $profile,
        "form" => $form->createView()
    ]);
}

避免视图中的广告是可以的,但在侧面视图中:

键为“0”的数组的键“名称”不存在。

发生什么事?这是相同的代码,但配置文件控制器不发送任何数据。

标签: phpsymfony

解决方案


代替

 $profile = $entityManager->getRepository(Profile::class)->findBy(["user" => $this->getUser()]);

$profile = $entityManager->getRepository(Profile::class)->findOneBy(["user" => $this->getUser()]);

我敢打赌,在您看来,您正在做类似profile.namewhilefindBy()正在返回一系列结果的事情,即使您只有一个结果。


推荐阅读