salut :).
voila je suis actuellement le tuto mvc de grafikart et j'essai de faire mon mvc a ma propre sauce. je viens de finir de coder mon model mais j'aimerai bien ajouter la possibiliter d'utiliser des associations comme dans cakephp mais je ne sais pas trop comment m'y prendre.
Quelqu'un pourrait me filer un coup de main please....

voila le code de mon model
[code]
<?php

class Model {

public $conf = 'default';
public $db;
static $connexions = [];
public $table = false ;
public $id ;
public function __construct(){

    $conf = Conf::$databases[$this->conf];

    if (isset(Model::$connexions[$this->conf])) {
        $this->db = Model::$connexions[$this->conf] = $pdo;
        return true;
    }
    try{

        $pdo = new PDO(
            'mysql:host='.$conf['host'].';dbname='.$conf['base'].'; ',
            $conf['login'],
            $conf['password'],
            [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'] );
        $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
        Model::$connexions[$this->conf] = $pdo;
        $this->db = $pdo;
    }catch(PDOException $e){
        die($e->getMessage());
    }

    if ($this->table === false) {

        $this->table = strtolower(get_class($this)).'s';
    }
}

public function find($req=null){

    $fields = '*';
    $limit = '';
    $order = "id DESC";

    if (isset($req['fields'])){$fields = $req['fields'];}
    $sql = 'select  '. $fields .' from '.$this->table.' as '.get_class($this).' ';

    if (isset($req['conditions'])) {
        $sql.='where ';

        if (!is_array($req['conditions'])) {
            $sql.=$req['conditions'];
        }else{

            $cond = [];
            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'])){
        $limit = 'limit '.$req['limit'];
        $sql  .= $limit;
    }
    if(isset($req['order'])){
        $order = $req['order'];
        $sql  .= $order;
    }

    $pre = $this->db->prepare($sql);
    $pre->execute();

    return $pre->fetchAll(PDO::FETCH_OBJ);

}

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

public function save($req){
    if (isset($req['id']) && !empty($req['id']) or isset($this->id) && !empty($this->id)) {
        $sql = 'update '.$this->table.' set ';

        $val = [];

        foreach ($req as $k => $v) {

            if ($k !="id") {
                $sql.="$k = ?,";
                $val[] = "$v";
            }

        }

        $sql = substr($sql,0,-1);

        $sql.= " where id=?";

        if (!isset($req['id']) and !empty($this->id)) {
            $val[] .= $this->id;
        }else{
            $val[] .= $req['id'];
        }

        $pre = $this->db->prepare($sql);
        $pre->execute($val);
    }else{

    $sql = 'insert into '.$this->table.' (';
        foreach ($req as $k => $v) {
            $sql .= "$k,";
        }

        $sql = substr($sql,0,-1);

        $val = [];

        $sql.=") values (";
        foreach ($req as $v) {

            $sql.="?,";
            $val[] = "$v";
        }
        $sql = substr($sql,0,-1);
        $sql.=")";

        // echo $sql; print_r($val);exit;
        $pre = $this->db->prepare($sql);
        $pre->execute($val);
    }
}

}
[/code]

Aucune réponse