19:08
Этот коммит содержится в:
39
frm/App.php
39
frm/App.php
@@ -17,12 +17,30 @@ class App {
|
||||
|
||||
protected $route_table;
|
||||
|
||||
function __construct() {
|
||||
public $dbh;
|
||||
|
||||
public $config;
|
||||
|
||||
function __construct( $config ) {
|
||||
$this->config = $config;
|
||||
date_default_timezone_set('Europe/Moscow');
|
||||
error_reporting(E_ALL);
|
||||
$this->user_agent = $_SERVER['HTTP_USER_AGENT'];
|
||||
$this->user_IP = $_SERVER['REMOTE_ADDR'];
|
||||
$this->setRoute( '|^/$|', 'Index', 'index' );
|
||||
|
||||
$dsn = 'mysql:host=' . $this->config->db->host
|
||||
. ';dbname=' . $this->config->db->dbname . ';charset=utf8';
|
||||
|
||||
try {
|
||||
$this->dbh = new \PDO(
|
||||
$dsn,
|
||||
$this->config->db->user,
|
||||
$this->config->db->password
|
||||
);
|
||||
} catch (\PDOException $exception) {
|
||||
die( 'Нет подключения к MySQL: ' . $exception->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,6 +61,18 @@ class App {
|
||||
return $this->user_IP;
|
||||
}
|
||||
|
||||
public function getPostObj( $array ) {
|
||||
$out = [];
|
||||
foreach ( $array as $value ) {
|
||||
if ( isset( $_POST[ $value ] ) ) {
|
||||
$out[ $value ] = $_POST[ $value ];
|
||||
} else {
|
||||
$out[ $value ] = false;
|
||||
}
|
||||
}
|
||||
return (object) $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Установка маршрута
|
||||
*
|
||||
@@ -64,6 +94,13 @@ class App {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
public function routeRun( $controller, $action = 'index', $params = [] ) {
|
||||
$a = $this->config->controller_namespace . $controller . 'Controller';
|
||||
$obj = new $a;
|
||||
call_user_func_array( [ $obj, $action . 'Action' ], $params );
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Запуск маршрутов
|
||||
*/
|
||||
|
62
frm/Model.php
Обычный файл
62
frm/Model.php
Обычный файл
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Игорь
|
||||
* Date: 24.01.2017
|
||||
* Time: 12:47
|
||||
*/
|
||||
|
||||
namespace PFRM;
|
||||
|
||||
|
||||
use PTEST\M\AdModel;
|
||||
|
||||
class Model {
|
||||
|
||||
protected $_table_name;
|
||||
|
||||
public $app;
|
||||
|
||||
function __construct() {
|
||||
global $app;
|
||||
$this->app = $app;
|
||||
|
||||
$this->_table_name = preg_replace( '/^.*\\\(.*)Model$/', "$1", get_called_class() );
|
||||
}
|
||||
|
||||
|
||||
public function save() {
|
||||
$operation = "INSERT INTO ";
|
||||
$suffix = "";
|
||||
if ( isset( $this->table_id ) ) {
|
||||
$operation = "UPDATE ";
|
||||
$suffix = " WHERE id = :id";
|
||||
}
|
||||
$table_columns = NULL;
|
||||
$pseudo_columns = NULL;
|
||||
foreach ($this as $key => $value ) {
|
||||
if ( preg_match('/^table_(?<name>.*)/', $key, $matches) ) {
|
||||
if ( $matches['name'] != 'id' && isset( $this->$key ) ) {
|
||||
$table_columns[] = $matches['name'];
|
||||
$pseudo_columns[] = ':' . $matches['name'];
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( is_array( $table_columns ) ) {
|
||||
$names = ' ( ' . implode( ',', $table_columns ) . ' ) ';
|
||||
$pseudo_names = ' ( ' . implode( ',', $pseudo_columns ) . ' ) ';
|
||||
$stmt = $this->app->dbh->prepare( $operation . $this->_table_name . $names . ' VALUES ' . $pseudo_names . $suffix );
|
||||
foreach ($table_columns as $value) {
|
||||
$tmp_str = 'table_' . $value;
|
||||
$stmt->bindParam( ':' . $value, $this->$tmp_str );
|
||||
}
|
||||
if ( isset( $this->table_id ) ) {
|
||||
$stmt->bindParam( ':id', $this->table_id );
|
||||
}
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
||||
|
||||
static public function findFirst( $condition ) {
|
||||
}
|
||||
}
|
Ссылка в новой задаче
Block a user