php - 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”的数组的键“名称”不存在。
发生什么事?这是相同的代码,但配置文件控制器不发送任何数据。
解决方案
代替
$profile = $entityManager->getRepository(Profile::class)->findBy(["user" => $this->getUser()]);
和
$profile = $entityManager->getRepository(Profile::class)->findOneBy(["user" => $this->getUser()]);
我敢打赌,在您看来,您正在做类似profile.name
whilefindBy()
正在返回一系列结果的事情,即使您只有一个结果。
推荐阅读
- javascript - SublimeREPL 不适用于 Javascript 代码
- if-statement - IF 的参数数量错误
- node.js - mongoose.populate() 返回一个空数组
- python - 在 Keras 上将两个模型的权重加载到一个中
- python-3.x - 如何正确绘制小浮点数
- swift - 单击按钮后如何更改变量 UITextView?
- javascript - 做出反应。如何将 arg 传递给 onClick 调用的函数而不自动触发它
- sql - SQL 语句中的 IN 运算符不起作用?
- request - Cheerio 在 Google Cloud 功能中未按预期工作
- python - 如何使用 BFS 和 DFS 计算两个网格坐标之间的最短路径