0

im working on a project with symfony3 and im beginner at this framework this is my problem i have a table in data base called groupe wher i have a colum called member in which i have stored the id's of members in this group table database

in the first step i would like to get those ids from the groupe table and then explode them to send queries to my database to get the nom,prenomand the profile pic that correspand to the id send so i have made two action in my repository

this is my first action in my repository which allow me to get string of id from table membre in my database

    public function getmemberstring($id){

    $membres = $this->getEntityManager()
        ->createQuery('SELECT g.membres FROM GroupGroupBundle:Groupe g  
            WHERE g.id =:id')
        ->setParameter('id',$id);



    return $membres->getResult();

} 

and this is my seccond function in my repository that allows me to extract id from the previous string and send request to database to get the information that i want

  public function getmemberlist($list){

    $memberId = explode('-',$list);


            $membres = array();
          foreach($memberId as $value){
                $nom = $this->getEntityManager()
                    ->createQuery('SELECT m.nom FROM MainBundle:Member m
                    WHERE m.id =:id')
                    ->setParameter('id',$value);
                 $nom = $nom->getResult();

              $prenom = $this->getEntityManager()
                  ->createQuery('SELECT m.prenom FROM MainBundle:Member m
                    WHERE m.id =:id')
                  ->setParameter('id',$value);
              $prenom = $prenom->getResult();


              $pic = $this->getEntityManager()
                  ->createQuery('SELECT m.profile_pic FROM MainBundle:Member m
                    WHERE m.id =:id')
                  ->setParameter('id',$value);
              $pic = $pic->getResult();


              array_push($membres,$nom,$prenom,$pic);


          }

    return $membres;

}

this is my controller

   public function membreGroupeAction()
{
    $em = $this->getDoctrine()->getManager();
    $str = $em->getRepository("GroupGroupBundle:Groupe")->findOneBy(array('id'=>1));
    $member = $em->getRepository("GroupGroupBundle:Groupe")->getmemberlist($str->getMembres());



    return $this->render('@GroupGroup/layout/membres.html.twig',array("mem"=>$member));

}

this is my twig view wher i want to show my send by the controller

  {% extends '@GroupGroup/Group/groupe_mur_base.html.twig' %}
  {% block panel %}
  {% for i in  mem %}
    {% for j in i %}
      {% for t in j  %}
        {{ t }}
      {% endfor %}
    {% endfor %}
  {% endfor %}
 <div class="jumbotron list-content" style="display: block;">
    <ul class="list-group">
        <li href="#" class="list-group-item title">
            Liste des Membres
        </li>
        <li href="#" class="list-group-item text-left" id="listmembre"      style="display: block;">
            <div class="image">
                <img class="img-thumbnail" src="http://bootdey.com/img/Content/User_for_snippets.png">
                <span id="membername">Juan guillermo cuadrado</span>
                <div><button id="btnajout" class="btn btn-primary">Ajouter</button></div>

            </div>
            <div class="break"></div>
        </li>

this is how my data appears in my template i want to style them and to get only the 3 item for each user separated in li list like the template belowmy template

Wassim Ben Hssen
  • 519
  • 6
  • 23

1 Answers1

1

You can do it all in one query

public function getmemberlist($list){

    $memberId = explode('-',$list);

    $membres = $this->getEntityManager()
        ->createQuery(
            'SELECT m.nom, m.prenom, m.profile_pic
             FROM MainBundle:Member m
             WHERE m.id IN(:id)'
        )
        ->setParameter('id',array_values($memberId))
        ->getResult()
    ;            

    return $membres;
}

or even better in a repository

public function getmemberlist($list){

    $memberId = explode('-',$list);

    $membres = $this->createQueryBuilder('m')
        ->where('m.id IN(:id)')
        ->setParameter('id',array_values($memberId))
        ->getQuery()
        ->getResult()
    ;            

    return $membres;
}

This way you will get a one dimension array containing all the users in your group.

Your twig

{% extends '@GroupGroup/Group/groupe_mur_base.html.twig' %}
    {% block panel %}
        <div class="jumbotron list-content" style="display: block;">
            <ul class="list-group">
                <li href="#" class="list-group-item title">
                    Liste des Membres
                </li>
                {% for m in mem %}
                    <li href="{{ path('your_path_to_member_details') }}" class="list-group-item text-left" id="listmembre"      style="display: block;">
                        <div class="image">
                            <img class="img-thumbnail" src="{{ asset(m.profile_pic) }}"/>
                            <span class="membername">
                                {{ m.prenom }} {{ m.nom }}
                            </span> 
                        </div>
                    <div class="break"></div>
                </li>
            {% endfor %}
        </ul>
        <div>
            <button id="btnajout" class="btn btn-primary">
                Ajouter
            </button>
        </div>
    </div>
{% endblock %}

Looking at your code it seems your french or belgian, you should go on OpenClassrooms Symfony Tutoriel to learn how to develop with Symfony

OlivierC
  • 682
  • 4
  • 11