Advertisement
villers

Untitled

Jun 17th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.99 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Exemple:
  5.  * ---------
  6.  * Insert
  7.  * ---------
  8.  * // method object:
  9.  * $user = new User();
  10.  * $user->name  = 'villers';
  11.  * $user->email = '[email protected]';
  12.  * $user->save();
  13.  *
  14.  * // method array
  15.  * $user = User();
  16.  * $user->insert(array('name' => 'villers', 'email' => '[email protected]'));
  17.  *
  18.  * Select
  19.  * ---------
  20.  * // method de selection de l'utilisateur id 1
  21.  * $user = new User(1);
  22.  *
  23.  * // method de selection d'un ou plusieurs utilisateurs avec des critères
  24.  * $users = new User();
  25.  * $users->where('active', '=', 1)->where('name', '=', 'villers')->limit(10)->find();
  26.  *
  27.  * Update
  28.  * --------
  29.  * // method object:
  30.  * $user = new User(1);
  31.  * $user->name = 'villers';
  32.  * $user->save();
  33.  *
  34.  * // method array
  35.  * $user = new User(1);
  36.  * $user->save(array('name' => 'villers'));
  37.  *
  38.  * // method array avec critères
  39.  * $user = new User();
  40.  * $user->where('id', '=', array(1, 2))->limit(2)->update(array('name' => 'vilers'));
  41.  *
  42.  * Delete
  43.  * --------
  44.  * // method object
  45.  * $user = new User();
  46.  * $user->delete(1);
  47.  *
  48.  * method array avec critères
  49.  * $user = new User();
  50.  * $user->where('id', '=', 1)->limit(1)->delete();
  51.  *
  52.  * Query
  53.  * ------------------------
  54.  * $this->run('SELECT * FROM `user` WHERE `name` = :name', array(':name' => 'villers'));
  55.  * $user = $this->fetch();
  56.  * echo 'Salut, ' . $user->name;
  57.  */
  58. class Model
  59. {
  60.     private $_connection;
  61.     private $_statement;
  62.     private $_select = array();
  63.     private $_from = array();
  64.     private $_where = array();
  65.     private $_having = array();
  66.     private $_order = array();
  67.     private $_limit = array();
  68.     private $_store = array();
  69.     private $_data;
  70.     protected $_primaryKey = 'id';
  71.  
  72.     /**
  73.     * Constructeur
  74.     *
  75.     * @param int⎢array $id du membre à rechercher
  76.     */
  77.     public function __construct($id = null)
  78.     {
  79.         if ($id)
  80.         {
  81.             $this->where($this->_primaryKey, '=', $id)
  82.                  ->limit(1)
  83.                  ->find();
  84.             $this->_store = $this->fetch(\PDO::FETCH_ASSOC);
  85.         }
  86.     }
  87.  
  88.     /**
  89.      * Connection a là database si elle n'est pas déjà effectué.
  90.      */
  91.     private function connect() {
  92.         //TODO
  93.     }
  94.  
  95.     /**
  96.      * Execute une requette SQL.
  97.      *
  98.      * @param  string  $sql  La requête a éxécuter.
  99.      * @param  array   $data Les données à préparer.
  100.      * @return boolean       true si rla requètte éussi
  101.      */
  102.     public function run($sql, $data = array()) {
  103.         if (! $this->_connection) {
  104.             $this->connect();
  105.         }
  106.  
  107.         // Prepare, execute, reset, et return le resultat
  108.         $this->_statement = $this->_connection->prepare($sql);
  109.         $result = $this->_statement->execute($data);
  110.         $this->reset();
  111.         return $result;
  112.     }
  113.  
  114.     /**
  115.      * Ajoute un filtre de selection
  116.      *
  117.      * @param  string $field Le nom du champ.
  118.      * @param  string $as    Le nom de remplacement
  119.      * @return Model
  120.      */
  121.     public function select($field, $as = null) {
  122.         $this->_select[] = array('field' => $field, 'as' => $as);
  123.         return $this;
  124.     }
  125.  
  126.     /**
  127.      * Ajoute une table a là requête
  128.      *
  129.      * @param  string $table Le nom de la table
  130.      * @return Model
  131.      */
  132.     public function from($table) {
  133.         $this->_from[] = $table;
  134.         return $this;
  135.     }
  136.  
  137.     /**
  138.      * Ajoute une condition a là requête
  139.      *
  140.      * // Une condition simple
  141.      * ->where('name', '=', 'villers')
  142.      *
  143.      * // Recherche / array
  144.      * ->where('name', 'IN', array('villers', 'mickael', 'ghota'));
  145.      * // ou
  146.      * ->where('name', '=',  array('villers', 'mickael', 'ghota'));
  147.      *
  148.      * // Une multiple condition
  149.      * ->where('name', '=', 'villers')->where('email', '=', '[email protected]')
  150.      *
  151.      * @param  string       $field    Le champ a tester.
  152.      * @param  string       $operator L'operateur de condition (=, >, etc.)
  153.      * @param  string|array $value    La valeur de la condition
  154.      * @param  string       $joiner   La façon de joindre
  155.      * @return Model
  156.      */
  157.     public function where($field, $operator, $value, $joiner = null) {
  158.         $this->_where[] = array(
  159.             'field'    => $field,
  160.             'operator' => $operator,
  161.             'value'    => $value,
  162.             'joiner'   => $joiner
  163.         );
  164.         return $this;
  165.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement