sa marche pas c tjrs le même message d'erreur
( ! ) Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\LaboMvc\mvc\AbriFramework\Model.php on line 79
Model.php :
<?php
class Model {
static $connections = array();
public $conf = 'default';
public $table = false;
public $primaryKey = 'id';
public $db;
// public $pdo;
public function __construct(){
// j'initialise quelque variable
if($this->table === false){
$this->table = strtolower(get_class($this)).'s';
}
// je me connecte a la base
$conf = Config::$databases$this->conf];
if(isset(Model::$connections$this->conf])){
$this->db = Model::$connections$this->conf];
return true;
}
try{
$pdo = new PDO(
'mysql: host='.$conf'host'].';dbname='.$conf'database'],
$conf'login'],
$conf'password'],
array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')
);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
Model::$connections$this->conf] = $pdo;
$this->db = $pdo;
return $this->db;
}catch(PDOException $e){
if(Config::$debug >= 1){
die($e->getMessage());
}else{
die('Impossible de se connecter à la base de donnée');
}
}
}
public function find($req){
$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).' ';
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'];
}
// die($sql);
// die(var_dump($this->db));
$pre = $this->db->prepare($sql);
$pre->execute();
return $pre->fetchAll(PDO::FETCH_OBJ);
}
public function findFirst($req){
return current($this->find($req));
}
public function findCount($conditions){
$res = $this->findFirst(array(
'fields' => 'COUNT('.$this->primaryKey.') as count',
'conditions' => $conditions
));
return $res->count;
}
}
PostsController.php :
<?php
class PostsController extends AbriController {
function index(){
$perPage = 1;
$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){
$this->loadModel('Post');
$d'page'] = $this->Post->findFirst(array(
'conditions' => array('online' => 1, 'type' => 'post')
));
if(empty($d'page'])){
$this->e404('Page introuvable');
}
$this->set($d);
}
}
ça marche pas tjrs