Bonjour,

Ce message ne me quitte pas depuis le début des tutos ...

J'ai revérifier les codes et je ne parviens pas à trouver la source du problème.

Au tuto numéro 6 il à fallu faire une nouvelle page "admin_index", au début la page s'affichait correctement mais dès que j'ai ajouté le :

<a href="<?php echo Router::url('admin/posts/edit/'.$v->id); ?>">Editer</a>

L'erreur est revenue ...

( ! ) Notice: Undefined property: PostsController::$Post in C:\wamp\www\Test\controller\PostsController.php on line 79
Call Stack
#   Time    Memory  Function    Location
1   0.0020  146760  {main}( )   ..\index.php:0
2   0.0350  246648  Dispatcher->__construct( )  ..\index.php:11
3   0.0420  273592  call_user_func_array ( )    ..\Dispatcher.php:18
4   0.0420  273776  PostsController->admin_edit( )  ..\Dispatcher.php:18
( ! ) Fatal error: Call to a member function findFirst() on a non-object in C:\wamp\www\Test\controller\PostsController.php on line 79
Call Stack
#   Time    Memory  Function    Location
1   0.0020  146760  {main}( )   ..\index.php:0
2   0.0350  246648  Dispatcher->__construct( )  ..\index.php:11
3   0.0420  273592  call_user_func_array ( )    ..\Dispatcher.php:18
4   0.0420  273776  PostsController->admin_edit( )  ..\Dispatcher.php:18

Voici le code ma page "admin_index.php":

<div class="page-header">
    <h1><?php echo $total; ?> Articles</h1>
</div>
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>En ligne ?</th>
            <th>Titre</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($posts as $k=>$v): ?>
            <tr>
                <td><?php echo $v->id; ?></td>
                <td><span class="label <?php echo($v->online==1)?'success':'error'; ?>"><?php echo ($v->online==1)?'En ligne':'Hors ligne'; ?></span></td>
                <td><?php echo $v->name; ?></td>
                <td>
                    <a href="<?php echo Router::url('admin/posts/edit/'.$v->id); ?>">Editer</a>
                    <a onclick="return confirm('Voulez vous vraiment supprimer ce contenu'); " href="<?php echo Router::url('admin/posts/delete/'.$v->id); ?>">Supprimer</a>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<a href="<?php echo Router::url('admin/posts/edit'); ?>" class="primary btn">Ajouter un article</a>

et celui de ma page "PostsController.php" :

<?php
class PostsController extends Controller{
    //Blog, liste les articles
    function index(){
        $perPage = 10;
        $this->loadModel('Post');
        $condition = array('online' => 1,'type'=>'post');
        $d'posts'] = $this->Post->find(array(
            'conditions' => $condition,
            'limit' => ($perPage*($this->request->page-1)).','.$perPage
        ));
        $d'total'] = $this->Post->findCount($condition);
        $d'page'] = ceil($d'total'] / $perPage);
        $this->set($d);
    }

    function view($id,$slug){
        $this->loadModel('Post');
        $d'post'] = $this->Post->findFirst(array(
            'fields' => 'id,slug,content,name',
            'conditions' => array('online' => 1, 'id' =>$id, 'type'=>'post')
        ));
        if(empty($d'post'])){
            $this->e404('Page introuvable');
        }
        if($slug != $d'post']->slug){
            $this->redirect("posts/view/id:$id/slug:".$d'post']->slug,301);
        }
        $this->set($d);
    }

    //Admin ACTIONS
    //Liste les différents articles
    function admin_index(){
        $perPage = 10;
        $this->loadModel('Post');
        $condition = array('type'=>'post');
        $d'posts'] = $this->Post->find(array(
            'fields' => 'id,name,online',
            'conditions'=> $condition,
            'limit' => ($perPage*($this->request->page-1)).','.$perPage
        ));
        $d'total'] = $this->Post->findCount($condition);
        $d'page'] = ceil($d'total'] / $perPage);
        $this->set($d);
    }

    //Permet d'éditer un article
    function admin_edit($id = null){
        $this->loadModel('Post');
        if($id === null){
            $post = $this->Post->findFirst(array(
                'conditions' => array('online' => -1)
            ));
            if(!empty($post)){
                $id = $post->id;
            }else{
                $this->Post->save(array(
                    'online' => -1
                ));
                $id = $this->Post->id;
            }
        }
        $d'id'] = $id;
        if($this->request->data){
            if($this->Post->validates($this->request->data)){
                $this->request->data->type = 'post';
                $this->request->data->created = date('Y-m-d h:i:s');

                $this->Post->save($this->request->data);
                $this->Session->setFlash('Le contenu a bien été modifié');
                $this->redirect('admin/posts/index');
            }else{
                $this->Session->setFlash('Merci de corriger vos informations','error');
            }

        }else{
            $this->request->data = $this->Post->findFirst(array(
                'conditions' => array('id'=>$id)
            ));
        }
        $this->set($d);
    }

    //Permet de supprimer un article
    function admin_delete($id){
        $this->loadModel('Post');
        $this->Post->delete($id);
        $this->Session->setFlash('Le contenu a bien été supprimé');
        $this->redirect('admin/posts/index');
    }

    //Permet de lister les contenus
    function admin_tinymce(){
        $this->loadModel('Post');
        $this->layout = 'modal';
        $d'posts'] = $this->Post->find();
        $this->set($d);
    }
}
?>

Et ci-dessous une partie du code de la page "Model.php" la ou l'erreur parle findFirst() :

public function find($req = array()){        
        $sql = 'SELECT ';

        if(isset($req'fields'])){
            if(is_array($req'fields'])){
                $sql .= implode(', ', $req'fields']);
            }else{
                $sql .= $req'fields'];
            }
        }else{
            $sql .= '*';
        }

        $sql .= ' FROM '.$this->table. ' as '.get_class($this).' ';

        // Construction de la condition
        if(isset($req'conditions'])){
            $sql .= 'WHERE ';
            if(!is_array($req'conditions'])){
                $sql .= $req'conditions'];
            }else{
               $cond = array();
               foreach($req'conditions'] as $k=>$v){
                   if(!is_numeric($v)){
                      $v = '"'.mysql_real_escape_string($v).'"';
                   }
                   $cond] = "$k=$v";
               }
               $sql .= implode(' AND ', $cond);
            }
        }

        if(isset($req'limit'])){
            $sql .= 'LIMIT '.$req'limit'];            
        }       

        $pre = $this->db->prepare($sql);
        $pre->execute();
        return $pre->fetchAll(PDO::FETCH_OBJ);        
    }

    public function findFirst($req){
        return current($this->find($req));
    }

Si vous avez un début de solution à me soumettre ...

Bonne journée à tous.

2 réponses


Jackisback
Auteur
Réponse acceptée

Salut,

Ouais je l'ai fait mais il me retourne rien si je ne modifie pas d'article sinon il me retourne l'id de l'article logique quoi ...

@ +

----------------------[EDIT]----------------------

Bon j'ai une fois de plus refait le code pour la nième fois et cette fois ça fonctionne donc sujet résolu.

Merci à ceux qui y ont réfléchis.

Bonsoir,
tu peux faire un debug($id) juste avant ça :

$this->request->data = $this->Post->findFirst(array(
'conditions' => array('id'=>$id)
));

(en restant dans le else)

cordialement

Antho