Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Exemple:
- * ---------
- * Insert
- * ---------
- * // method object:
- * $user = new User();
- * $user->name = 'villers';
- * $user->email = '[email protected]';
- * $user->save();
- *
- * // method array
- * $user = User();
- * $user->insert(array('name' => 'villers', 'email' => '[email protected]'));
- *
- * Select
- * ---------
- * // method de selection de l'utilisateur id 1
- * $user = new User(1);
- *
- * // method de selection d'un ou plusieurs utilisateurs avec des critères
- * $users = new User();
- * $users->where('active', '=', 1)->where('name', '=', 'villers')->limit(10)->find();
- *
- * Update
- * --------
- * // method object:
- * $user = new User(1);
- * $user->name = 'villers';
- * $user->save();
- *
- * // method array
- * $user = new User(1);
- * $user->save(array('name' => 'villers'));
- *
- * // method array avec critères
- * $user = new User();
- * $user->where('id', '=', array(1, 2))->limit(2)->update(array('name' => 'vilers'));
- *
- * Delete
- * --------
- * // method object
- * $user = new User();
- * $user->delete(1);
- *
- * method array avec critères
- * $user = new User();
- * $user->where('id', '=', 1)->limit(1)->delete();
- *
- * Query
- * ------------------------
- * $this->run('SELECT * FROM `user` WHERE `name` = :name', array(':name' => 'villers'));
- * $user = $this->fetch();
- * echo 'Salut, ' . $user->name;
- */
- class Model
- {
- private $_connection;
- private $_statement;
- private $_select = array();
- private $_from = array();
- private $_where = array();
- private $_having = array();
- private $_order = array();
- private $_limit = array();
- private $_store = array();
- private $_data;
- protected $_primaryKey = 'id';
- /**
- * Constructeur
- *
- * @param int⎢array $id du membre à rechercher
- */
- public function __construct($id = null)
- {
- if ($id)
- {
- $this->where($this->_primaryKey, '=', $id)
- ->limit(1)
- ->find();
- $this->_store = $this->fetch(\PDO::FETCH_ASSOC);
- }
- }
- /**
- * Connection a là database si elle n'est pas déjà effectué.
- */
- private function connect() {
- //TODO
- }
- /**
- * Execute une requette SQL.
- *
- * @param string $sql La requête a éxécuter.
- * @param array $data Les données à préparer.
- * @return boolean true si rla requètte éussi
- */
- public function run($sql, $data = array()) {
- if (! $this->_connection) {
- $this->connect();
- }
- // Prepare, execute, reset, et return le resultat
- $this->_statement = $this->_connection->prepare($sql);
- $result = $this->_statement->execute($data);
- $this->reset();
- return $result;
- }
- /**
- * Ajoute un filtre de selection
- *
- * @param string $field Le nom du champ.
- * @param string $as Le nom de remplacement
- * @return Model
- */
- public function select($field, $as = null) {
- $this->_select[] = array('field' => $field, 'as' => $as);
- return $this;
- }
- /**
- * Ajoute une table a là requête
- *
- * @param string $table Le nom de la table
- * @return Model
- */
- public function from($table) {
- $this->_from[] = $table;
- return $this;
- }
- /**
- * Ajoute une condition a là requête
- *
- * // Une condition simple
- * ->where('name', '=', 'villers')
- *
- * // Recherche / array
- * ->where('name', 'IN', array('villers', 'mickael', 'ghota'));
- * // ou
- * ->where('name', '=', array('villers', 'mickael', 'ghota'));
- *
- * // Une multiple condition
- * ->where('name', '=', 'villers')->where('email', '=', '[email protected]')
- *
- * @param string $field Le champ a tester.
- * @param string $operator L'operateur de condition (=, >, etc.)
- * @param string|array $value La valeur de la condition
- * @param string $joiner La façon de joindre
- * @return Model
- */
- public function where($field, $operator, $value, $joiner = null) {
- $this->_where[] = array(
- 'field' => $field,
- 'operator' => $operator,
- 'value' => $value,
- 'joiner' => $joiner
- );
- return $this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement