Set et Find.

Par Warkas Creation, il y a 11 ans


Bonjour,

Encore moi comme tous les jours maintenant :

Ma views ne trouve pas ma variable $posts alors que je l'est set dans mon Controller :

    $this->loadModel("Post");
    $this->set("posts", $this->Post->find("all"));

Dans ma view :

<?php foreach($posts as $post): ?> <div class="c"> <h3><?= $post["Posts"]["title"] ?><span style="padding-left: 6px;">[ <?php echo $this->Html->link($post["Posts"]["author"], '/profile/Author'); ?> ]</span></h3> <p><?php echo substr($post["Posts"]["content"], 0, 172); echo $this->Html->link('...', '/posts'); ?></p> </div> <?php endforeach; ?>

Et les erreurs sur la page sont :

Undefined variable: posts [APP\View\Pages\home.ctp, line 16] Warning (2): Invalid argument supplied for foreach() [APP\View\Pages\home.ctp, line 16]

Merci beaucoup encore pour votre compréhension,
et excusez moi du dérangement.

21 réponses

JC_Pires, il y a 11 ans

essai ça:

$this->loadModel('Post'); $posts = $this->Post->find('all'); $this->set(compact('posts'));
Muxabble, il y a 11 ans

Tu est sur Cake 2 ou 3 ?
Normalement si tu est dans le controller Posts tu ne doit pas load le model car il est déjà load par defaut :

// Cake 3 $posts = $this->Posts->find('all'); $this->set('posts', $posts); // Cake 2 $posts = $this->Post->find('all'); $this->set('posts');
Warkas Creation, il y a 11 ans

Je ne suis pas dans le PostController c'est pour ca que je specifie le model a utilisé.
Je suis dans ma page d'accueil et je suis sur cake php 2.0

Muxabble, il y a 11 ans

Eh bien tu fait

$this->loadModel('Post'); $posts = $this->Post->find('all'); $this->set('posts');
Warkas Creation, il y a 11 ans

Et dans ma view "/pages/home.ctp" ? Je met quoi ?

Muxabble, il y a 11 ans

Ton premier code

<?php foreach($posts as $post): ?> <div class="c"> <h3><?= $post["Post"]["title"] ?><span style="padding-left: 6px;">[ <?php echo $this->Html->link($post["Post"]["author"], '/profile/Author'); ?> ]</span></h3> <p><?php echo substr($post["Post"]["content"], 0, 172); echo $this->Html->link('...', '/posts'); ?></p> </div> <?php endforeach; ?>

En enlevant les S à ton model !

Warkas Creation, il y a 11 ans

Ah bas non toujours les mêmes erreur. Il y a un soucis bizarre pourquoi il ne trouve pas la variable posts.

Warkas Creation, il y a 11 ans

Vous pensez que c'est du a que ca soit la "PagesController" ?

<?php /** * Static content controller. * * This file will render views from views/pages/ * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://cakephp.org CakePHP(tm) Project * @package app.Controller * @since CakePHP(tm) v 0.2.9 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ App::uses('AppController', 'Controller'); /** * Static content controller * * Override this controller by placing a copy in controllers directory of an application * * @package app.Controller * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html */ class PagesController extends AppController { /** * This controller does not use a model * * @var array */ public $uses = array(); /** * Displays a view * * @return void * @throws NotFoundException When the view file could not be found * or MissingViewException in debug mode. */ public function display() { $path = func_get_args(); $count = count($path); if (!$count) { return $this->redirect('/'); } $page = $subpage = $title_for_layout = null; if (!empty($path[0])) { $page = $path[0]; } if (!empty($path[1])) { $subpage = $path[1]; } if (!empty($path[$count - 1])) { $title_for_layout = Inflector::humanize($path[$count - 1]); } $this->set(compact('page', 'subpage', 'title_for_layout')); try { $this->render(implode('/', $path)); } catch (MissingViewException $e) { if (Configure::read('debug')) { throw $e; } throw new NotFoundException(); } // Je commence mon code $this->loadModel('Post'); $posts = $this->Post->find('all'); $this->set(compact('posts')); } }
Warkas Creation, il y a 11 ans

Toujours la même chose, je commence a désesperer.

Si vous voulez en voir plus je veux bien vous laissé venir voir sur Teamviewer.

Warkas Creation, il y a 11 ans

J'ai copié exactement ton code et j'ai remplacé mais ca me fais toujours "Undefined variable : posts [APP\View\Pages\home.ctp, line 17]"

JC_Pires, il y a 11 ans

Allez courcircuitons le problème

dans tes routes

Router::connect( '/', array('controller' => 'pages', 'action' => 'accueil') );

Dans ton controller pages apres la function display

public function accueil() { $this->loadModel('Post'); $posts = $this->Post->find('all'); $this->set(compact('posts')); }

On créé une vue dans le dossier view/pages/accueil.ctp

<?php foreach($posts as $post): ?> <?php debug($post);?> <?php endforeach; ?>

Et là?

Warkas Creation, il y a 11 ans
Notice (8): Undefined variable: posts [APP\View\Pages\accueil.ctp, line 1] Warning (2): Invalid argument supplied for foreach() [APP\View\Pages\accueil.ctp, line 1]

Dans le content voilà.. Bon les frameworks m'aiment pas.

JC_Pires, il y a 11 ans

J'ai l'impression que ta variable $posts est vide ... tu es sûr qu'il trouve des enregistrements?

public function accueil() { $this->loadModel('Post'); $posts = $this->Post->find('all'); debug($posts); die(); $this->set(compact('posts')); }
JC_Pires, il y a 11 ans

Si tu veut je veut bien t'aider par teamviewer comme tu le suggerais, mais comment ce donner les infos "ID et MDP" de façon sécurisée?

Warkas Creation, il y a 11 ans

Pas un moyen de MP sur grafikart ? Twitter en MP comme tu le souhaite.

JC_Pires, il y a 11 ans

Dans la section live: http://www.grafikart.fr/live il ya un chat ou on peut ce parler en privé juste à coté de la 1ere vidéo "Cakephp 3.0" il ya une fenetre irc ou il te demande de renseigner ton identifiant une fois fais il te connectera j'y suis je t'attends

Muxabble, il y a 11 ans

Ta view c'est home.ctp ou accueil.ctp ?

JC_Pires, il y a 11 ans

Problème réglé.

Muxabble, il y a 11 ans

C'étais du à quoi du coup ?

JC_Pires, il y a 11 ans

Une faute de frappe, et pour la méthode avec la fonction accueil, dans la route il avais mis:

Router::connect( '/', array('controller' => 'pages', 'action' => 'accueil', 'home') il fallais supprimer home );

Sinon pour la première méthode dans la fonction display(), il ne fallais pas le mettre dans

if (!$count) { return $this->redirect('/'); }

Mais juste avant le try catch "ce qui est logique ..."

try { $this->render(implode('/', $path)); } catch (MissingViewException $e) { if (Configure::read('debug')) { throw $e; } throw new NotFoundException(); }
Muxabble, il y a 11 ans

Il utilise des try catch intéressant x) , ah oui il avait foutu deux action pas de soucis alors, faudrais mettre le sujet en resolu x)