我在Ajax中有一个自动完成功能来处理名为Mentor
的实体,该实体可以拥有lastName
和firstName
,这两个实体都可以自动完成。
我需要在我的控制器功能中添加两个返回。
这是我为一个return
做的事情,它有效(我为last name
做了)
public function ajaxAutoCompleteMentorAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$lastNames = $em->getRepository('AppBundle:Mentor')->getMentorLastAutocomplete($request->query->get('term'));
$formattedData = array();
foreach ($lastNames as $lastName) {
$formattedData[] = array(
'value' => $lastName['lastName'],
);
}
return new JsonResponse($formattedData);
}
然后我尝试这样做为firstName
字段添加一个返回值。像那样
public function ajaxAutoCompleteMentorAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$lastNames = $em->getRepository('AppBundle:Mentor')->getMentorLastAutocomplete($request->query->get('term'));
$firstNames = $em->getRepository('AppBundle:Mentor')->getMentorFirstAutocomplete($request->query->get('term'));
$formattedLast = array();
foreach ($lastNames as $lastName) {
$formattedLast[] = array(
'value' => $lastName['lastName'],
);
}
return new JsonResponse($formattedLast);
$formattedFirst = array();
foreach ($firstNames as $firstName) {
$formattedFirst[] = array(
'value' => $firstName['firstName'],
);
}
return new JsonResponse($formattedFirst);
}
但我无法使其发挥作用。我应该把if
放在哪里?
谢谢你的帮助
答案 0 :(得分:1)
您可以删除两个退货并仅返回:
return new JsonResponse([$formattedFirst, $formattedLast]);