Symfony 4 用户通过电子邮件搜索问题
Symfony 4 user search by email issues
我正在尝试在页面管理员的仪表板中实现通过电子邮件搜索用户的功能。目前,我已经将一个值硬编码到 $email
变量中,只是为了测试搜索是否有效。它确实找到了正确的用户,但没有在 twig 中显示任何内容。
执行 {{ dump() }} 输出:array:2 [▼ 0 => 用户 {#4745 ▼ -id: 5 -
用户名:"test_user" -plainPassword:null -密码:
“$2y$13$rGYteIrzifg9Dty.O5knOOCHQnzOtF.nZux8h1jc4sNbap5V7Xn0。” -电子邮件:
"tester@test.com" } "app" => AppVariable {#2617 ▶} ]
我在AdminController.php中使用的函数:
/**
* @Route("/admin/result", name="user_search")
* Method({"POST"})
*/
public function user_search(Request $request)
{
$email = 'tester@test.com';
$result = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(['email' => $email]);
if ($result) {
return $this->render('admin/result.html.twig', $result);
}else{
return $this->render('admin/result.html.twig', [
'error' => 'No user found with this email '.$email]);
}}
result.html.twig:
{% extends 'base.html.twig' %}
{% block body %}
{% if error %}
<span class="error">{{ error }}</span>
{% endif %}
{% if result %}
<table>
<tr>
<th>Username</th><th>Email</th>
</tr>
{% for item in result %}
<tr>
<td>{{ item.getUsername }}</td><td>{{ item.getEmail }}
</td>
</tr>
{% endfor %}
</table>
{% endif %}
{{ dump() }}
{% endblock %}
检查 $user instance
if ($result instanceof User)
.......................................................................
我建议你在 twig
中使用 defined
{% if result is defined %}
{% extends 'base.html.twig' %}
{% block body %}
{% if error is defined %}
<span class="error">{{ error }}</span>
{% else %}
<table>
<tr>
<th>Username</th><th>Email</th>
</tr>
{% for item in result %}
<tr>
<td>{{ result.getUsername }}</td><td>{{ result.getEmail }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{% endblock %}
在 twig 中,您假设结果是一个数组。为此,使用 findBy 而不是 findOneBy。 findBy returns 具有所需搜索的对象数组。 findOneBy return 仅具有所需搜索的对象,如果未找到结果,则为 null。
示例:
// look for a single User by email
$result = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(['email' => $email]);
// look for multiple User objects matching the email
$result = $this->getDoctrine()
->getRepository(User::class)
->findBy(['email' => $email]);
最终通过以下步骤解决了这个问题:
- 硬编码
$email
变量中存在拼写错误。
- 更改为
return $this->render('admin/result.html.twig', 'result'->$result);
而不是 return $this->render('admin/result.html.twig', $result);
- 更改为
<td>{{ item.username }}</td><td>{{ item.email }}</td>
而不是 <td>{{ result.getUsername }}</td><td>{{ result.getEmail }}</td>
我正在尝试在页面管理员的仪表板中实现通过电子邮件搜索用户的功能。目前,我已经将一个值硬编码到 $email
变量中,只是为了测试搜索是否有效。它确实找到了正确的用户,但没有在 twig 中显示任何内容。
执行 {{ dump() }} 输出:array:2 [▼ 0 => 用户 {#4745 ▼ -id: 5 - 用户名:"test_user" -plainPassword:null -密码: “$2y$13$rGYteIrzifg9Dty.O5knOOCHQnzOtF.nZux8h1jc4sNbap5V7Xn0。” -电子邮件: "tester@test.com" } "app" => AppVariable {#2617 ▶} ]
我在AdminController.php中使用的函数:
/**
* @Route("/admin/result", name="user_search")
* Method({"POST"})
*/
public function user_search(Request $request)
{
$email = 'tester@test.com';
$result = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(['email' => $email]);
if ($result) {
return $this->render('admin/result.html.twig', $result);
}else{
return $this->render('admin/result.html.twig', [
'error' => 'No user found with this email '.$email]);
}}
result.html.twig:
{% extends 'base.html.twig' %}
{% block body %}
{% if error %}
<span class="error">{{ error }}</span>
{% endif %}
{% if result %}
<table>
<tr>
<th>Username</th><th>Email</th>
</tr>
{% for item in result %}
<tr>
<td>{{ item.getUsername }}</td><td>{{ item.getEmail }}
</td>
</tr>
{% endfor %}
</table>
{% endif %}
{{ dump() }}
{% endblock %}
检查 $user instance
if ($result instanceof User)
.......................................................................
我建议你在 twig
中使用defined
{% if result is defined %}
{% extends 'base.html.twig' %}
{% block body %}
{% if error is defined %}
<span class="error">{{ error }}</span>
{% else %}
<table>
<tr>
<th>Username</th><th>Email</th>
</tr>
{% for item in result %}
<tr>
<td>{{ result.getUsername }}</td><td>{{ result.getEmail }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{% endblock %}
在 twig 中,您假设结果是一个数组。为此,使用 findBy 而不是 findOneBy。 findBy returns 具有所需搜索的对象数组。 findOneBy return 仅具有所需搜索的对象,如果未找到结果,则为 null。
示例:
// look for a single User by email
$result = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(['email' => $email]);
// look for multiple User objects matching the email
$result = $this->getDoctrine()
->getRepository(User::class)
->findBy(['email' => $email]);
最终通过以下步骤解决了这个问题:
- 硬编码
$email
变量中存在拼写错误。 - 更改为
return $this->render('admin/result.html.twig', 'result'->$result);
而不是return $this->render('admin/result.html.twig', $result);
- 更改为
<td>{{ item.username }}</td><td>{{ item.email }}</td>
而不是<td>{{ result.getUsername }}</td><td>{{ result.getEmail }}</td>