191107 0048
Этот коммит содержится в:
@@ -18,6 +18,10 @@ class App
|
||||
* @var Config
|
||||
*/
|
||||
public $config;
|
||||
/**
|
||||
* @var \PDO
|
||||
*/
|
||||
public $db = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
@@ -13,5 +13,6 @@ class DefaultController extends Controller
|
||||
public function index()
|
||||
{
|
||||
(new View())->index();
|
||||
$Model = new \MyApp\Model\GroupsModel();
|
||||
}
|
||||
}
|
||||
|
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace MyApp\Core;
|
||||
|
||||
use PDOException;
|
||||
use ReflectionClass;
|
||||
use ReflectionProperty;
|
||||
use PDO;
|
||||
|
||||
class Model
|
||||
{
|
||||
@@ -11,12 +13,47 @@ class Model
|
||||
* @var array
|
||||
*/
|
||||
private $internalTableMap;
|
||||
/**
|
||||
* @var \App
|
||||
*/
|
||||
protected $app;
|
||||
|
||||
public function __construct(?array $tableMap = null)
|
||||
{
|
||||
if (!is_array($tableMap)) {
|
||||
global $app;
|
||||
$this->app = $app;
|
||||
|
||||
if ( ! is_array($tableMap)) {
|
||||
$this->setInternalTableMap();
|
||||
}
|
||||
|
||||
if ('db' === $this->getModelType()) {
|
||||
$this->initDb();
|
||||
}
|
||||
}
|
||||
|
||||
private function initDb(): void
|
||||
{
|
||||
if (is_null($this->app->db)) {
|
||||
try {
|
||||
$this->app->db = new PDO(
|
||||
'mysql:host=mysql;dbname='
|
||||
. $this->app->config->getDbName()
|
||||
. ';port='
|
||||
. $this->app->config->getDbPort(),
|
||||
$this->app->config->getDbUser(),
|
||||
$this->app->config->getDbPassword(),
|
||||
array(
|
||||
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8',
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
|
||||
PDO::ATTR_ERRMODE => true,
|
||||
PDO::ATTR_PERSISTENT => true,
|
||||
));
|
||||
} catch (PDOException $e) {
|
||||
echo 'Подключение не удалось: ' . $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private function setInternalTableMap()
|
||||
@@ -24,10 +61,10 @@ class Model
|
||||
/* @var \App $app */
|
||||
global $app;
|
||||
|
||||
$tableMap = ['type' => 'bd'];
|
||||
$tableMap = ['type' => 'db'];
|
||||
|
||||
$refClass = new ReflectionClass(get_called_class());
|
||||
$tags = $app->parseTagsFromComment($refClass->getDocComment(),
|
||||
$tags = $app->parseTagsFromComment($refClass->getDocComment(),
|
||||
'TableName');
|
||||
if ($tags) {
|
||||
if (array_key_exists('TableName', $tags)) {
|
||||
@@ -56,9 +93,16 @@ class Model
|
||||
$this->internalTableMap = array_key_exists('id',
|
||||
$tableMap) ? $tableMap : ['type' => 'other'];
|
||||
}
|
||||
if ( ! is_array($this->internalTableMap)) {
|
||||
$this->internalTableMap = ['type' => 'other'];
|
||||
}
|
||||
}
|
||||
|
||||
public function find($id)
|
||||
public static function find()
|
||||
{
|
||||
}
|
||||
|
||||
public static function findOne()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -69,4 +113,9 @@ class Model
|
||||
{
|
||||
return $this->internalTableMap;
|
||||
}
|
||||
|
||||
public function getModelType(): string
|
||||
{
|
||||
return $this->internalTableMap['type'];
|
||||
}
|
||||
}
|
||||
|
77
src/models/GroupsModel.php
Обычный файл
77
src/models/GroupsModel.php
Обычный файл
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace MyApp\Model;
|
||||
|
||||
use MyApp\Core\Model;
|
||||
|
||||
/**
|
||||
* Class GroupsModel
|
||||
*
|
||||
* @TableName = "groups"
|
||||
*
|
||||
* @package MyApp\Model
|
||||
*/
|
||||
class GroupsModel extends Model
|
||||
{
|
||||
/**
|
||||
* @ColumnName = "id"
|
||||
* @ColumnOption = "id"
|
||||
* @ColumnType = "int unsigned auto_increment primary key"
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
/**
|
||||
* @ColumnName = "name"
|
||||
* @ColumnType = "varchar(255) null"
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
/**
|
||||
* @ColumnName = "parent_id"
|
||||
* @ColumnType = "int unsigned null"
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $parent;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return GroupsModel
|
||||
*/
|
||||
public function setName(string $name): self
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $parent
|
||||
*
|
||||
* @return GroupsModel
|
||||
*/
|
||||
public function setParent(int $parent): self
|
||||
{
|
||||
$this->parent = $parent;
|
||||
return $this;
|
||||
}
|
||||
}
|
13
src/models/ServersModel.php
Обычный файл
13
src/models/ServersModel.php
Обычный файл
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace MyApp\Model;
|
||||
|
||||
|
||||
class ServersModel
|
||||
{
|
||||
private $id;
|
||||
private $name;
|
||||
private $ip;
|
||||
private $group;
|
||||
}
|
Ссылка в новой задаче
Block a user