hi i want to select the info by the file link , for example in database in first line :

file link :  src/java/son3.wav | word : mailing 

secund line in DB : file link : src/java/son3.wav  | word: smtp
3rd line in DB  : file link : src/java/son2.wav | word : server

I want to show like this in the html.twig ::

id : 1 | file link : src/java/son3.wav | word : mailing , smtp
id : 2 | file link : src/java/son2.wav | word : server

so that select all the words in DB that have the same file link, I hope this explains what I'm after.

I'm using symfony 3.4 and this is how it showed in my interface :

https://i.imgur.com/9roxlOA.jpg

this is pic of my table in DB :

enter image description here

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>



and this is the code of file controller :

  public function IndexAction()
    {


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

    }

}

1 Answers

1
Rein Baarsma On

Just condense the results afterward in plain php.

$viewResults = [];
foreach ($resultatas as $result) {
    if (!isset($viewResults[$result->getIndexeMot()->getMotValeur())]) {
        $viewResults[$result->getIndexeMot()->getMotValeur()] = ['id' => $result->getId(), 'motValeurs' => []];
    }
    $viewResults[$result->getIndexeMot()->getMotValeur()]['motValeurs'][] = $result->getIndexeFichier()->getFichierUrl();
}

And in the view separate them as you like:

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

PS: If you use the Symfony Profiler, you will see that your usage of findAll() will probably add two additional queries PER result, because the result is not properly joined. It would be better to store the results directly in indexe, or to join the two other tables through a doctrine query: getRepository(Indexe::class)->createQueryBuilder('q')->join('q.indexeMot', 'm')->join('q.indexeFichier')->getResult();