Bonjour ou bonsoir à tous,

J'ai un problème au niveau de la 6ème minutes de la vidéo.
Voici l'erreur que j'ai Notice: Undefined property: Post::$pdo in /home/cercleescrimegarennois/www/core/Model.php on line 52 Fatal error: Call to a member function prepare() on a non-object in /home/cercleescrimegarennois/www/core/Model.php on line 52

Voici le code de Model:

<?php
class Model{

    static $connections = array();

    public $conf = 'default';
    public $table = false;
    public $db;

    public function __construct(){
        if($this->table === false){
        $this->table = strtolower(get_class($this)).'s';
        }
        $conf = Conf::$databases$this->conf];
        if(isset(Model::$connections$this->conf])){
            $this->db = Model::$connections$this->conf];
            return true;
        }
        try{
            $this->pdo = new PDO('mysql:host='.$conf'host'].';dbname='.$conf'database'].';'
                ,$conf'login'],$conf'password'],array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
            Model::$connections$this->conf] = $this->pdo;
        }catch(PDOException $e){
            if(Conf::$debug >= 1){
                die($e->getMessage());
            }else{
                die('Impossible de se connecter à la base de donnée');
            }
        }

    }
    public function find($req){
        $this->sql = 'SELECT * FROM '.$this->table.' as '.get_class($this).' ';
        if(isset($req'conditions'])){
            $this->sql .= 'WHERE ';
            if(!is_array($req'conditions'])){
                $this->sql .= $req'conditions'];
            }else{
                $cond = array();
                foreach($req'conditions'] as $k=>$v){
                    if(!is_numeric($v)){
                        $v = '"'.mysql_escape_string($v).'"';
                    }
                    $cond] = "$k=$v";
                }
                $this->sql .= implode(' AND ',$cond);
            } 
        }
        $pre = $this->pdo->prepare($this->sql);
        $pre->execute();
        return $pre->fetchAll(PDO::FETCH_OBJ);
    }
    public function findFirst($req){
        return current($this->find($req));
    }
}

Merci d'avance pour les réponses.

3 réponses


RedaElkhayat
Réponse acceptée

Ajouter avant la fonction __construct une variable de $pdo

<?php
    public $pdo;
?>
titilgc
Auteur

Merci pour la réponse le problème est résolu. Mais l'erreur Call to a member function prepare() on a non-object apparaît aussitôt ;-)

As-tu corriger ce problème depuis tout ce temps ?