MARSHAL327

Untitled

Oct 15th, 2021 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.69 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace app\core;
  5.  
  6. use PDO;
  7. use PDOException;
  8.  
  9. class BaseActiveRecord extends Model
  10. {
  11.     public static $pdo;
  12.  
  13.     protected static $tablename;
  14.     protected static $dbfields = [];
  15.  
  16.     public function __construct() {
  17.         parent::__construct();
  18.  
  19.         if (!static::$tablename) {
  20.             return;
  21.         }
  22.  
  23.         static::setupConnection();
  24.         static::getFields();
  25.         $this->fillFields();
  26.     }
  27.  
  28.     public static function getFields() {
  29.         $stmt = static::$pdo->query("SHOW FIELDS FROM " . static::$tablename);
  30.         while ($row = $stmt->fetch()) {
  31.             static::$dbfields[$row['Field']] = $row['Type'];
  32.         }
  33.     }
  34.  
  35.     public function fillFields(){
  36.         foreach (static::$dbfields as $field => $type){
  37.             $this->$field;
  38.         }
  39.     }
  40.  
  41.     public static function setupConnection() {
  42.         if (!isset(static::$pdo)) {
  43.             try {
  44.                 static::$pdo = new PDO("mysql:dbname=Web-LR; host=localhost; charset=utf8", "root", "");
  45.             } catch (PDOException $ex) {
  46.                 die("Ошибка подключения к БД: $ex");
  47.             }
  48.         }
  49.     }
  50.  
  51.     public static function find($fieldValue, $searchField = "id") {
  52.         $sql = "SELECT * FROM " . static::$tablename . " WHERE `$searchField`='$fieldValue'";
  53.         $stmt = static::$pdo->query($sql);
  54.         $row = $stmt->fetch(PDO::FETCH_ASSOC);
  55.  
  56.         if (!$row) return null;
  57.  
  58.         $ar_obj = new static();
  59.  
  60.         foreach ($row as $key => $value) {
  61.             $ar_obj->$key = $value;
  62.         }
  63.  
  64.         return $ar_obj;
  65.     }
  66.  
  67.     public static function all($orderBy = "id", $orderDirection = "ASC"){
  68.         $sql = "SELECT * FROM " . static::$tablename . " ORDER BY $orderBy $orderDirection";
  69.         return static::get($sql);
  70.     }
  71.  
  72.     private static function get($sql){
  73.         $stmt = static::$pdo->query($sql);
  74.         $tableData = $stmt->fetchAll(PDO::FETCH_ASSOC);
  75.  
  76.         if (!$tableData) return null;
  77.  
  78.         $records = [];
  79.  
  80.         foreach ($tableData as $row) {
  81.             $ar_obj = new static();
  82.  
  83.             foreach ($row as $key => $value) {
  84.                 $ar_obj->$key = $value;
  85.             }
  86.  
  87.             $records[] = $ar_obj;
  88.         }
  89.  
  90.         return $records;
  91.     }
  92.  
  93.     public static function paginate($numPage){
  94.         if (isset($_GET['page'])){
  95.             $page = $_GET['page'];
  96.         } else $page = 1;
  97.         $recordStart = ($page * $numPage) - $numPage;
  98.  
  99.         $sql = "SELECT * FROM " . static::$tablename . " LIMIT $numPage OFFSET $recordStart";
  100.         return static::get($sql);
  101.     }
  102.  
  103.     public static function getNumRow(){
  104.         $sql = "SELECT COUNT(*) as count FROM " . static::$tablename;
  105.         return static::$pdo->query($sql)->fetchColumn();
  106.     }
  107.  
  108.     public function save() {
  109.         foreach (static::$dbfields as $field => $type){
  110.             $holder = $this->$field;
  111.             static::$dbfields[$field] = $holder;
  112.         }
  113.  
  114.         $dbfields = "`" . implode('`, `', array_keys(static::$dbfields)) . "`";
  115.         $prepareStr = ":" . implode(', :', array_keys(static::$dbfields));
  116.         unset(static::$dbfields['id']);
  117.         static::$dbfields['id'] = NULL;
  118.  
  119.         $sql = "INSERT INTO " . static::$tablename . " ($dbfields) VALUES ($prepareStr)";
  120.         static::$pdo->prepare($sql)->execute(static::$dbfields);
  121.     }
  122.  
  123.     public function delete() {
  124.         $sql = "DELETE FROM " . static::$tablename . " WHERE ID=" . $this->id;
  125.         $stmt = static::$pdo->query($sql);
  126.         if ($stmt) {
  127.             return $stmt->fetch(PDO::FETCH_ASSOC);
  128.         } else {
  129.             print_r(static::$pdo->errorInfo());
  130.         }
  131.     }
  132. }
Add Comment
Please, Sign In to add comment