首页 > 解决方案 > 如何在同一个文件中选择多个单词,它们具有相同的文件 wav?

问题描述

ii想通过文件链接选择信息,例如在第一行的数据库中:

文件链接:src/java/son3.wav | 词:邮寄

DB 中的第二行:文件链接:src/java/son3.wav | 词:smtp

DB 中的第 3 行:文件链接:src/java/son2.wav | 词:服务器

我想在 html.twig :: 中显示这样的内容

编号:1 | 文件链接:src/java/son3.wav | word : 邮寄 , smtp
id : 2 | 文件链接:src/java/son2.wav | 词:服务器

以便选择数据库中具有相同文件链接的所有单词,我希望这能解释我所追求的。

我正在使用 symfony 3.4,这就是它在我的界面中的显示方式:

https://i.stack.imgur.com/7EQ63.jpg

这是我在 DB 中的表的图片:

https://i.stack.imgur.com/MEYaJ.jpg

this is the code of file twig :

  <table  class="table table-striped">
          <thead>
              <tr>
                 <th>Id</th>
                  <th>File Link</th>
                  <th>Words</th>

              </tr>
          </thead>
          <tbody>

          {% for result in resultats %}
                <tr>
                  <td><a href="{{ path('result', { 'id': result.id }) }}">{{ result.id }}</a></td>
                  <td>{{ result.indexeFichier.fichierUrl }}</td>
                  <td>{{ result.indexeMot.motValeur }}</td>




              </tr>
          {% endfor %}
          </tbody>
      </table>

这是文件控制器的代码:


  public function IndexAction()
    {


      ////////////////////////////////////////////
      $em = $this->getDoctrine()->getManager();
      $resultats = $em->getRepository('AppBundle:Indexe')->findAll();
      return $this->render('userfiles/result.html.twig', array(
          'resultats' => $resultats,
      ));

    }

标签: phpsqlsymfony

解决方案


我认为仅使用 DQL 查询是不可能的,后处理可以提供帮助:

公共函数 IndexAction() {

  ////////////////////////////////////////////
  $em = $this->getDoctrine()->getManager();

  $tmp = $em->getRepository('AppBundle:Indexe')->findAll();
  $tmp2 = [];
  foreach ($tmp as $v) {
      if (!isset($resultats[$v->getIndexeFichier()->getFichierUrl()])) {
          $resultats[$v->getIndexeFichier()->getFichierUrl()] = [];
      }
      $resultats[$v->getIndexeFichier()->getFichierUrl()][] = $v->indexeMot()->getMotValeur();

  }

  $resultats = [];
  $i = 1;
  foreach ($tmp2 as $k=>$v) {
      $resultats[] = ['id' => $i++, 'fichierUrl' => $k, 'motValeur' => join(', ', $v)];
  }

  return $this->render('userfiles/result.html.twig', array(
      'resultats' => $resultats,
  ));

}

并在模板中

  ....
  {% for result in resultats %}
        <tr>
          <td><a href="{{ path('result', { 'id': result.id }) }}">{{ result.id }}</a></td>
          <td>{{ result.fichierUrl }}</td>
          <td>{{ result.motValeur }}</td>
      </tr>
  {% endfor %}
  ....

如果你的数据库是mysql,你可以尝试使用GROUP_CONCAT()函数


推荐阅读