【问题标题】:Symfony 4 user search by email issuesSymfony 4 用户通过电子邮件问题进行搜索
【发布时间】:2023-03-28 05:32:01
【问题描述】:

我正在尝试通过电子邮件功能在页面管理员的仪表板中实现对用户的搜索。目前,我已经将一个值硬编码到 $email 变量中,只是为了测试搜索是否有效。它确实找到了正确的用户,但没有在树枝中显示任何内容。

执行 {{ dump() }} 输出:array:2 [▼ 0 => User {#4745 ▼ -id: 5 - 用户名:“test_user” -plainPassword:空 -password: “$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 %}

【问题讨论】:

  • 尝试使用 {% if result is defined %}
  • @D.Dimitrov 谢谢,这解决了部分问题。现在我得到了渲染的树枝,但显然 $result 总是未定义的。
  • 您能否编辑或发布 {{ dump() }} 在您的 twig 文件中输出的内容?
  • @D.Dimitrov dump() 的输出是:array:2 [▼ "error" => "No user found with this email test@tester.com" "app" => AppVariable { #2617 ▶} ]
  • 如果你启用了调试模式,你能看到数据库到底准备了什么查询吗?或者你有什么错误?

标签: php symfony twig php-7 symfony4


【解决方案1】:

在 twig 中,您假设结果是一个数组。为此,使用 findBy 而不是 findOneBy。 findBy 返回具有所需搜索的对象数组。 findOneBy 仅返回具有所需搜索的对象,如果未找到结果,则返回 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]);

【讨论】:

    【解决方案2】:

    最后通过以下步骤解决了:

    1. 硬编码的$email 变量中有错字。
    2. 更改为 return $this-&gt;render('admin/result.html.twig', 'result'-&gt;$result); 而不是 return $this-&gt;render('admin/result.html.twig', $result);
    3. 更改为 &lt;td&gt;{{ item.username }}&lt;/td&gt;&lt;td&gt;{{ item.email }}&lt;/td&gt; 而不是 &lt;td&gt;{{ result.getUsername }}&lt;/td&gt;&lt;td&gt;{{ result.getEmail }}&lt;/td&gt;

    【讨论】:

      【解决方案3】:

      instance检查$user

      if ($result instanceof User)
      .......................................................................
      

      我建议你在树枝中使用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 %}
      

      【讨论】:

      • 谢谢,这解决了部分问题。现在我得到了渲染的树枝,但显然 $result 总是未定义的。
      • 你的意思是($result instanceof User)吗?
      • instanceof 不是 twig 函数。
      • 我是为twig写的吗? ))
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      相关资源
      最近更新 更多