IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

Parcourir un objet [2.x]


Sujet :

Symfony PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Homme Profil pro
    Inscrit en
    Juin 2007
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2007
    Messages : 63
    Par défaut Parcourir un objet
    Bonjour,

    J'ai une boucle for et j'aimerai dans Twig afficher le nom de la catégorie dans lequel se trouve mon post. Mais je n'arrive pas à accéder à cette donnée.

    Voila le dump de mon posts :
    array (size=3)
      0 => 
        object(Chan\ChanBundle\Entity\Post)[283]
          private 'id' => int 3
          private 'name' => string 'La dernière chance' (length=19)
          private 'slug' => string 'la-derniere-chance' (length=18)
          private 'image' => string 'lol.jpg' (length=7)
          private 'author' => string 'Moi' (length=3)
          private 'date' => 
            object(DateTime)[257]
              public 'date' => string '2013-08-19 10:26:51' (length=19)
              public 'timezone_type' => int 3
              public 'timezone' => string 'UTC' (length=3)
          private 'ncomments' => int 0
          private 'category' => 
            object(Chan\ChanBundle\Entity\Category)[280]
              private 'id' => int 1
              private 'name' => string 'Test' (length=4)
              private 'slug' => string 'test' (length=4)
      1 => etc...
    J'essaye donc de prendre le category.name.

    J'aurai pensé faire comme suit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    {% for post in posts %}
        <div class="feed_text">
            <p><a href="#">{{ post.name }}</a> {{ post.category.name }} </p>
        </div>
    {% else %}
    Mais il m'envoie sur les roses :-°

    Impossible to access an attribute ("name") on a NULL variable ("") in ChanChanBundleefault:index.html.twig at line 22
    Merci et désolé pour cette question de naab

  2. #2
    Membre Expert
    Homme Profil pro
    Inscrit en
    Septembre 2009
    Messages
    875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Septembre 2009
    Messages : 875
    Par défaut
    il ne peut pas accéder a un attribut sur une variable nulle. Donc il y'a un problème dans ton code a un moment précis ton poste n'a pas de categorie (donc il ne peut pas acceder a l'attribut name).

  3. #3
    Membre actif
    Homme Profil pro
    Inscrit en
    Juin 2007
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2007
    Messages : 63
    Par défaut
    Hmmmm c'est grave docteur ?

    Pourtant mon code on ne peut pas faire plus simple :

    Controller
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?php
     
    class DefaultController extends Controller
    {
        public function indexAction() {
     
            $posts = $this->getDoctrine()->getManager()->getRepository('ChanChanBundle:Post')->getLastPosts();
     
            return $this->render('ChanChanBundle:Default:index.html.twig', array('posts' => $posts));
        }
    Mon repository post :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?php
    class PostRepository extends EntityRepository
    {
        public function getLastPosts() {
     
        $q = $this->createQueryBuilder('p')
                      ->leftJoin('p.category', 'c')
                      ->addSelect('c')
                      ->orderBy('p.date', 'DESC');
     
        return $q->getQuery()->getResult();
      }
    }
    Et mon twig complet :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    {% extends "::base.html.twig" %}
     
    {% block title %}chan{% endblock %}
    {% block description %}Un test de hotgeart{% endblock %}
    {% block keywords %}hotgeart, test, 123{% endblock %}
     
    {% block body %}
    <div class="row">
        <div class="span8">
        	<h3>Les 10 derniers topics</h3>
     
            {% for post in posts %}
            <div class="corgi_feed_well">
                <div class="individual_feed_item">
                    <div class="feed_item">
                        <div class="feed_body">
                            <div class="row">
                                <div class="feed_profile_pic">
                                    <img src="{{ post.image }}" alt="meta image" class="meta_image" />
                                </div>
                                <div class="feed_text">
                                    <p><a href="#">{{ post.name }}</a> {{ post.category.name }} </p>
                                </div>
                            </div>
                        </div>
                        <hr class="feed_hr" />
                        <div class="bottom_meta">
                            <div class="row">
                                <div class="bottom_left">
                                    <div class="share_wrapper">
                                    </div>
                                </div>
                                <div class="bottom_right">
                                    <a href="#">{{ post.author }}</a> <span>|</span> <a href="#" class="show_comment_link">{{ post.ncomments }} comments</a> <span>|</span> {{ post.date|date('d/m/Y') }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% else %}
            <div class="corgi_feed_well">
                <div class="individual_feed_item">
                    <div class="feed_item">
                        <div class="feed_body">
                            <div class="row">
                                <p style="text-align:center;">Aucun post :'(</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>     
            {% endfor %}
     
        </div>
     
        {{ render(controller("ChanChanBundle:Category:list")) }}
    </div>
    {% endblock %}
    Il arrive à prendre tout sauf ce fameux nom de category

  4. #4
    Membre Expert
    Homme Profil pro
    Inscrit en
    Septembre 2009
    Messages
    875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Septembre 2009
    Messages : 875
    Par défaut
    En même temps un left join ne garantie pas que t'aura une catégorie pour chaque poste donc regarde ta base ou change ta requête

    http://www.w3schools.com/sql/sql_join_left.asp

  5. #5
    Membre actif
    Homme Profil pro
    Inscrit en
    Juin 2007
    Messages
    63
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2007
    Messages : 63
    Par défaut
    Effectivement my bad je regardais au mauvais endroit. Un ->innerJoin(); règle le soucis thx

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Parcourir un objet
    Par ragnarokr dans le forum C#
    Réponses: 17
    Dernier message: 01/06/2014, 17h42
  2. [POO] parcourir un objet json avec une boucle for in
    Par bucheron007 dans le forum Général JavaScript
    Réponses: 13
    Dernier message: 19/01/2009, 12h55
  3. Parcourir un objet Collection
    Par 3KyNoX dans le forum C#
    Réponses: 8
    Dernier message: 30/11/2008, 22h14
  4. Parcourir un objet
    Par Manitobaa dans le forum Windows Forms
    Réponses: 1
    Dernier message: 30/10/2008, 16h43
  5. parcourir un objet
    Par Jasmine80 dans le forum Langage
    Réponses: 9
    Dernier message: 13/06/2008, 11h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo