Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: askanim
- * Date: 04.06.2016
- * Time: 18:03
- */
- namespace System\Http\Model;
- use System\Db\Db;
- class Model
- {
- protected $tableName;
- protected $Select = 'SELECT ';
- protected $Insert = 'INSERT INTO ';
- protected $Update = 'UPDATE ';
- protected $Delete = 'DELETE FROM ';
- protected $where = '';
- protected $joinSelect = '';
- protected $on = '';
- protected $join = '';
- protected $order = '';
- protected $limit = '';
- //Функция выбора Таблицы из БД.
- public function table($tableName)
- {
- $this->tableName = $tableName;
- return $this;
- }
- //Функция экранирующая данные при добавлении
- public function prepareDataBase ($arr) {
- $db = Db::getConnection();
- $stringIns = [];
- foreach ($arr as $key => $value) {
- $stringIns[] = sprintf('%s=%s', $key, $db->quote($value));
- };
- return $stringIns = implode(', ', $stringIns);
- }
- //Функция добавления данных в таблицу БД
- public function Insert($arr) {
- $db = Db::getConnection();
- $stringIns = $this->prepareDataBase($arr);
- $result = $db->query($this->Insert . $this->tableName . ' SET ' . $stringIns);
- }
- //Функция обновления данных в таблице БД
- public function Update($arr, $where = NULL) {
- $db = Db::getConnection();
- $stringUp = $this->prepareDataBase($arr);
- if ($where != Null) {
- $this->where = $this->whereIs($where);
- }
- $result = $db->query($this->Update . $this->tableName . ' SET ' . $stringUp . $this->where);
- }
- //Функция Удаления данных из таблицы в БД
- public function del($where = NULL) {
- $db = Db::getConnection();
- if ($where != Null) {
- $this->where = $this->whereIs($where);
- }
- $db->query($this->Delete . $this->tableName . $this->where);
- }
- //Общая функция условия при выполнении запроса к таблицы БД
- public function whereIs ($arr) {
- $db = Db::getConnection();
- $stringVal = '';
- foreach ($arr as $key => $value) {
- if ($value == 'и') {
- $value = '&&';
- };
- if ($value == 'или') {
- $value = '||';
- };
- $stringVal = $stringVal . ' ' . $value ;
- }
- //echo $stringVal;
- $this->where = ' WHERE '. $stringVal;
- return $this->where;
- }
- //Функция условия при выполнении запроса к таблицы БД по средством цепочки
- public function where($arr)
- {
- $this->where = $this->whereIs($arr);
- return $this;
- }
- // Функции соединения
- private function Join ($nameTable, $join ,$arr = []) {
- $stringVal = '';
- foreach ($arr as $key => $value) {
- $stringVal = $stringVal . ', '. $value ;
- }
- $this->joinSelect = $stringVal;
- //echo $join.' JOIN '. $nameTable . . '<br>';
- $this->join = $join.' JOIN '. $nameTable;
- }
- public function fullJoin ($nameTable, $arr = []) {
- $this->Join($nameTable, ' FULL', $arr);
- return $this;
- }
- public function innerJoin ($nameTable, $arr = []) {
- $this->Join($nameTable, ' INNER', $arr);
- return $this;
- }
- public function leftJoin ($nameTable, $arr = []) {
- $this->Join($nameTable, ' LEFT', $arr);
- return $this;
- }
- public function rightJoin ($nameTable, $arr = []) {
- $this->Join($nameTable, ' RIGHT', $arr);
- return $this;
- }
- //Функция сортировки
- public function on ($string) {
- $this->on = ' ON '.$string.' ';
- //echo $this->on;
- return $this;
- }
- public function order($string) {
- $this->order = ' ORDER BY '.$string.' ';
- return $this;
- }
- public function limit($string) {
- $this->limit = ' LIMIT '.$string;
- return $this;
- }
- //Функция получения строк из таблицы
- public function get($arr = ['*'])
- {
- $db = Db::getConnection();
- $stringVal = '';
- foreach ($arr as $key => $value) {
- $stringVal = $stringVal . ', '. $value ;
- };
- $stringVal = trim($stringVal, ', ');
- //echo $this->Select . $stringVal . $this->joinSelect . ' FROM ' . $this->tableName. $this->join. $this->on . $this->where . $this->order . $this->limit.'<br>';
- $result = $db->prepare($this->Select . $stringVal . $this->joinSelect . ' FROM ' . $this->tableName. $this->join. $this->on . $this->where . $this->order . $this->limit);
- $result->execute();
- $baseArray = $result->fetchAll();
- return $baseArray;
- }
- public function getClass () {
- $className = get_class($this);
- $className = explode('\\', $className);
- $className = array_pop($className);
- return $className;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement