Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: askanim
  5. * Date: 04.06.2016
  6. * Time: 18:03
  7. */
  8.  
  9. namespace System\Http\Model;
  10.  
  11. use System\Db\Db;
  12.  
  13. class Model
  14. {
  15. protected $tableName;
  16. protected $Select = 'SELECT ';
  17. protected $Insert = 'INSERT INTO ';
  18. protected $Update = 'UPDATE ';
  19. protected $Delete = 'DELETE FROM ';
  20. protected $where = '';
  21. protected $joinSelect = '';
  22. protected $on = '';
  23. protected $join = '';
  24. protected $order = '';
  25. protected $limit = '';
  26.  
  27.  
  28. //Функция выбора Таблицы из БД.
  29. public function table($tableName)
  30. {
  31. $this->tableName = $tableName;
  32. return $this;
  33. }
  34.  
  35. //Функция экранирующая данные при добавлении
  36. public function prepareDataBase ($arr) {
  37. $db = Db::getConnection();
  38.  
  39. $stringIns = [];
  40.  
  41. foreach ($arr as $key => $value) {
  42. $stringIns[] = sprintf('%s=%s', $key, $db->quote($value));
  43.  
  44. };
  45.  
  46.  
  47. return $stringIns = implode(', ', $stringIns);
  48.  
  49. }
  50.  
  51. //Функция добавления данных в таблицу БД
  52. public function Insert($arr) {
  53.  
  54. $db = Db::getConnection();
  55.  
  56. $stringIns = $this->prepareDataBase($arr);
  57.  
  58. $result = $db->query($this->Insert . $this->tableName . ' SET ' . $stringIns);
  59.  
  60.  
  61.  
  62.  
  63. }
  64.  
  65.  
  66. //Функция обновления данных в таблице БД
  67.  
  68. public function Update($arr, $where = NULL) {
  69.  
  70. $db = Db::getConnection();
  71.  
  72. $stringUp = $this->prepareDataBase($arr);
  73.  
  74.  
  75. if ($where != Null) {
  76. $this->where = $this->whereIs($where);
  77.  
  78. }
  79.  
  80.  
  81. $result = $db->query($this->Update . $this->tableName . ' SET ' . $stringUp . $this->where);
  82.  
  83. }
  84.  
  85. //Функция Удаления данных из таблицы в БД
  86. public function del($where = NULL) {
  87.  
  88.  
  89. $db = Db::getConnection();
  90.  
  91. if ($where != Null) {
  92. $this->where = $this->whereIs($where);
  93. }
  94.  
  95. $db->query($this->Delete . $this->tableName . $this->where);
  96.  
  97. }
  98.  
  99. //Общая функция условия при выполнении запроса к таблицы БД
  100. public function whereIs ($arr) {
  101. $db = Db::getConnection();
  102. $stringVal = '';
  103.  
  104. foreach ($arr as $key => $value) {
  105. if ($value == 'и') {
  106. $value = '&&';
  107. };
  108. if ($value == 'или') {
  109. $value = '||';
  110. };
  111. $stringVal = $stringVal . ' ' . $value ;
  112. }
  113.  
  114.  
  115. //echo $stringVal;
  116. $this->where = ' WHERE '. $stringVal;
  117. return $this->where;
  118. }
  119. //Функция условия при выполнении запроса к таблицы БД по средством цепочки
  120. public function where($arr)
  121. {
  122. $this->where = $this->whereIs($arr);
  123.  
  124. return $this;
  125.  
  126. }
  127. // Функции соединения
  128.  
  129. private function Join ($nameTable, $join ,$arr = []) {
  130.  
  131. $stringVal = '';
  132.  
  133. foreach ($arr as $key => $value) {
  134. $stringVal = $stringVal . ', '. $value ;
  135. }
  136. $this->joinSelect = $stringVal;
  137. //echo $join.' JOIN '. $nameTable . . '<br>';
  138. $this->join = $join.' JOIN '. $nameTable;
  139.  
  140. }
  141. public function fullJoin ($nameTable, $arr = []) {
  142.  
  143. $this->Join($nameTable, ' FULL', $arr);
  144.  
  145. return $this;
  146.  
  147. }
  148. public function innerJoin ($nameTable, $arr = []) {
  149.  
  150. $this->Join($nameTable, ' INNER', $arr);
  151.  
  152. return $this;
  153.  
  154. }
  155. public function leftJoin ($nameTable, $arr = []) {
  156. $this->Join($nameTable, ' LEFT', $arr);
  157.  
  158. return $this;
  159. }
  160. public function rightJoin ($nameTable, $arr = []) {
  161. $this->Join($nameTable, ' RIGHT', $arr);
  162.  
  163. return $this;
  164. }
  165. //Функция сортировки
  166. public function on ($string) {
  167.  
  168. $this->on = ' ON '.$string.' ';
  169. //echo $this->on;
  170. return $this;
  171. }
  172. public function order($string) {
  173. $this->order = ' ORDER BY '.$string.' ';
  174. return $this;
  175. }
  176. public function limit($string) {
  177. $this->limit = ' LIMIT '.$string;
  178. return $this;
  179. }
  180. //Функция получения строк из таблицы
  181. public function get($arr = ['*'])
  182. {
  183.  
  184. $db = Db::getConnection();
  185.  
  186. $stringVal = '';
  187.  
  188. foreach ($arr as $key => $value) {
  189.  
  190. $stringVal = $stringVal . ', '. $value ;
  191.  
  192. };
  193. $stringVal = trim($stringVal, ', ');
  194.  
  195. //echo $this->Select . $stringVal . $this->joinSelect . ' FROM ' . $this->tableName. $this->join. $this->on . $this->where . $this->order . $this->limit.'<br>';
  196.  
  197. $result = $db->prepare($this->Select . $stringVal . $this->joinSelect . ' FROM ' . $this->tableName. $this->join. $this->on . $this->where . $this->order . $this->limit);
  198.  
  199. $result->execute();
  200.  
  201.  
  202. $baseArray = $result->fetchAll();
  203.  
  204. return $baseArray;
  205. }
  206. public function getClass () {
  207. $className = get_class($this);
  208.  
  209.  
  210. $className = explode('\\', $className);
  211.  
  212. $className = array_pop($className);
  213.  
  214. return $className;
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement