Problème comment en index

Par arcoss 3x, il y a 9 ans


Bonjour,
Je suis en train de mettre en place un système de commentaire et je rencontre un problème dans mon fichier show (fichier qui s'occupe d'aficher le post et qui affiche le système de commentaire) et j'ai cette erreur :

Notice (8): Undefined index: Comment [APP\View\Posts\show.ctp, line 49] Notice (8): Undefined index: Comment [APP\View\Posts\show.ctp, line 49]

Show.ctp (ligne 49) :

<h3><?= sprintf(__n('%s comment', '%s Comments', count($post['Comment'])), count($post['Comment'])); ?></h3>

Merci d'avance pour votre aide ;)

17 réponses

arcoss 3x, il y a 9 ans

Désolé le voici :

if(!empty($this->request->data)){ $this->request->data['Comment']['post_id'] = $post['Post']['id']; $this->Post->Comment->create($this->request->data, true); if($this->Post->Comment->save(null, true, array('mail', 'username', 'content', 'post_id'))){ $this->Session->setFlash("Thanks for your comment","success"); return $this->redirect($this->referer()); }else{ $this->Session->setFlash("You have to correct your errors first","error"); } }
arcoss 3x, il y a 9 ans

Le voilà !

<?php foreach ($post['Comment'] as $k => $comment): ?> <div class="row"> <div class="col-md-2"> <img src="http://1.gravatar.com/avatar/<?= md5($comment['mail']); ?>?s=100" width="100%"> </div> <div class="col-md-10"> <p><strong><?= h($comment['username']); ?></strong> <?= $this->Time->timeAgoInWords($comment['created'], array('end' => '1 an')); ?> ago</p> <p><?= nl2br(h($comment['content'])); ?></p> </div> </div> <hr> <?php endforeach ?>
arcoss 3x, il y a 9 ans

ça ne donne rien mais dans ma bdd tout y est correctement par contre j'ai une autre erreur que je n'avais as vu :

Notice (8): Undefined index: Comment [APP\View\Posts\show.ctp, line 51] Warning (2): Invalid argument supplied for foreach() [APP\View\Posts\show.ctp, line 51]

ligne 51 :

<?php foreach ($post['Comment'] as $k => $comment): ?>
arcoss 3x, il y a 9 ans
public $hasMany = array( 'Comment' => array( 'order' => 'Comment.created DESC' ) );

?

arcoss 3x, il y a 9 ans

Dans le model Post ou Comment ?

arcoss 3x, il y a 9 ans

Maintenant oui mais ca change rien ...

arcoss 3x, il y a 9 ans

Je vais essayer demain ! Merci de l'aide !

Lartak, il y a 9 ans

Bonsoir.
N'aurais-tu pas l'impression de tout confondre ?
Tu nous dit que ton problème concerne l'affichage de données, tu nous montre bien le code de ta vue correspondante, mais tu nous montre du code de ton controller qui concerne une sauvegarde en base de données.

arcoss 3x, il y a 9 ans

Oui je me suis dis ma meme chose ... Mais on apprend de ses erreurs....

arcoss 3x, il y a 9 ans

C'est bon je n'ai plus les 4 erreurs sur la vue de mon post en corrigeant le CONTROLLER. Mais mes commentaires ne s'affichent toujours pas ...

function show($id = null,$slug = null){ $this->Post->contain('Category', 'User', 'Comment'); if(!$id) throw new NotFoundException('Aucunes pages ne correspond à cet ID'); $post = $this->Post->find('first',array( 'conditions' => array('Post.id' => $id), 'recursive' => 0 )); if(empty($post)) throw new NotFoundException('Aucunes pages ne correspond à cet ID'); if($slug != $post['Post']['slug']) $this->redirect($post['Post']['link'],301); $d['post'] = $post; $this->set($d); $comments = $this->Post->Comment->find('all', [ 'conditions' => [ 'Comment.post_id' => $post['Post']['id'], ], 'fields' => [ 'Comment.id', //'Comment.user_id', 'Comment.content', 'Comment.created', //'Comment.parent_id', //'User.username', //'User.avatar', //'User.id', 'Comment.username', 'Comment.mail' ] ]); $this->set($comments); // Comment submission if(!empty($this->request->data)){ $this->request->data['Comment']['post_id'] = $post['Post']['id']; $this->Post->Comment->create($this->request->data, true); if($this->Post->Comment->save(null, true, array('mail', 'username', 'content', 'post_id'))){ $this->Session->setFlash("Thanks for your comment","success"); return $this->redirect($this->referer()); }else{ $this->Session->setFlash("You have to correct your errors first","error"); } } }

Et quand je décommente User.username, il me dit qu'il est inconnu dans fields list alors qu'il est présent dans ma bdd.

massadu33, il y a 9 ans

Bonjour,
Quelle est la valeur de ton $post['Post]['id'] ?
Essaye de mettre ton contain dans ta requête

$comments = $this->Post->find('all', [ 'contain' => [ 'Comment' ], 'conditions' => [ 'Comment.post_id' => $post['Post']['id'] ]
arcoss 3x, il y a 9 ans

la valeur de mon $post dans l article de test ? Et si je mets mon contain dans ma requete, il me remet les 4 erreurs ...

flashios09, il y a 9 ans

Bonjour arcoss 3x,
ça fait plus qu'un an que je n'ai pas utilisé cakephp2, essaye ça

function show($id = null, $slug = null) { if ($id === null) { throw new NotFoundException('Aucunes pages ne correspond à cet ID'); } $data = $this->Post->find('first', [ 'fields' => ['seulement_les_champs_de_la_table_posts'], 'conditions' => ['Post.id' => $id], // il faut avoir le containable behavior dans model/Post.php(voir la doc de cakephp2) 'contain' => [ 'Comment' => [ 'fields' => ['seulement_les_champs_de_la_table_comments'], // (!) il n'y a pas de limite pour l'imbrication par exemple on peux aussi recupérer le User du commentaire 'User' => [ 'fields' => ['seulement_les_champs_de_la_table_users'] ] ] ], 'recursive' => '0|1', // (!) à tester ]); // tu peux faire un debug ici pour voir le résultat de `$data` debug($data); if (!$data) { throw new NotFoundException('Aucunes pages ne correspond à cet ID'); } if ($slug != $data['Post']['slug']) { $this->redirect($data['Post']['link'],301); // je pense qu'il faut avoir un return après `->redirect` // (!) à tester return; } $this->set($data); }
arcoss 3x, il y a 9 ans

Je reçois les donnés de mon post dans mon debug mais il y a beaucoup d'érreurs ... Mais l'erreur récurente c'est celle-ci :

Notice (8): Undefined variable: post [APP\View\Posts\show.ctp, line 1]
Lartak, il y a 9 ans

Bonjour.
Il te suffit de faire quelque chose comme par exemple :

$post = $this->Post->find('first', [ 'conditions' => ['Post.id' => $id], 'contain' => [ 'Comment' => [ 'fields' => [ 'Comment.id', 'Comment.content', 'Comment.created', 'Comment.user_id', 'Comment.parent_id', 'Comment.username', 'Comment.mail' ] ], 'User' => ['fields' => ['User.id', 'User.username', 'User.avatar']], 'Category' ] ] );

Tu n'oublies pas bien sur de vérifier que la requête te retourne bien un résultat.

arcoss 3x, il y a 9 ans

Non c'est bon problème réglé !!! :

function show($id = null,$slug = null){ $this->Post->contain('Category', 'User','Comment'); if(!$id) throw new NotFoundException('Aucunes pages ne correspond à cet ID'); $post = $this->Post->find('first',array( 'conditions' => array('Post.id' => $id), 'recursive' => 0 )); if(empty($post)) throw new NotFoundException('Aucunes pages ne correspond à cet ID'); if($slug != $post['Post']['slug']) $this->redirect($post['Post']['link'],301); $d['post'] = $post; $this->set($d); // Comment submission if(!empty($this->request->data)){ $this->request->data['Comment']['post_id'] = $post['Post']['id']; $this->Post->Comment->create($this->request->data, true); if($this->Post->Comment->save(null, true, array('mail', 'username', 'content', 'post_id'))){ $this->Session->setFlash("Thanks for your comment","success"); return $this->redirect($this->referer()); }else{ $this->Session->setFlash("You have to correct your errors first","error"); } } }

Merci pour votre aide !!!

Lartak, il y a 9 ans

Le pb c'est que sur la page ou il récupère les posts il m'affiche aussi les pages ...

Tu veux dire que tu as fait un système de pages et de posts dans la même table, avec un champ dans lequel tu définies soit page soit post ?
Si c'est le cas, il te suffit par exemple de rajouter en condition 'type' => 'post'.