dbmsv4 init...5

This commit is contained in:
최준흠 2026-02-04 11:18:22 +09:00
parent 777ea0f2ff
commit fa39511b13
27 changed files with 307 additions and 100 deletions

View File

@ -9,7 +9,13 @@ class BoardEntity extends CommonEntity
{
const PK = Model::PK;
const TITLE = Model::TITLE;
protected array $nullableFields = [
'user_uid',
'worker_uid',
];
protected $attributes = [
'user_uid' => null,
'worker_uid' => null,
'category' => '',
'title' => '',
'status' => '',
@ -19,15 +25,15 @@ class BoardEntity extends CommonEntity
{
parent::__construct($data);
}
final public function getUserUid(): int|null
public function getUserUid(): int|null
{
return $this->user_uid ?? null;
}
final public function getWorkerUid(): int|null
public function getWorkerUid(): int|null
{
return $this->worker_uid ?? null;
}
final public function getCaregory(): string
public function getCaregory(): string
{
return $this->category ?? "";
}

View File

@ -8,45 +8,83 @@ abstract class CommonEntity extends Entity
{
protected $datamap = [];
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
//사용법 : $client->created_at->format('Y-m-d')
//비교방법 : if ($client->created_at < new \DateTime('2024-01-01')) {
/**
* 엔티티에서 "빈문자/공백 입력은 NULL로 저장"해야 하는 필드 목록.
* 기본은 배열이고, Entity에서 필요한 것만 override해서 채우면 .
*/
protected array $nullableFields = [];
public function __construct(array|null $data = null)
{
parent::__construct($data);
}
public function __get(string $key)
final public function __get(string $key)
{
if (array_key_exists($key, $this->attributes)) {
return $this->attributes[$key];
}
return parent::__get($key);
}
/**
* 중요: Entity에 값이 들어오는 "모든 경로" (new Entity($data), fill(), $entity->field=...)
* 에서 공통 규칙을 적용하기 위해 __set을 정의.
*/
final public function __set(string $key, $value = null)
{
if (array_key_exists($key, $this->attributes)) {
// 이 엔티티에서 NULL로 보정할 필드만 처리 (화이트리스트)
if (!empty($this->nullableFields) && in_array($key, $this->nullableFields, true)) {
if (is_string($value)) {
$value = trim($value);
}
$this->attributes[$key] = ($value === '' || $value === null) ? null : $value;
return;
}
// 기본: 그대로 저장
$this->attributes[$key] = $value;
return;
}
parent::__set($key, $value);
}
final public function getPK(): int|string
{
$field = constant("static::PK");
return $this->attributes[$field] ?? "";
}
final public function getTitle(): string
{
$field = constant("static::TITLE");
return $this->attributes[$field] ?? "";
}
public function getCustomTitle(): string
{
return $this->getTitle();
}
final public function getStatus(): string
{
return $this->status ?? "";
}
final public function getUpdatedAt(): string
{
return $this->updated_at ?? "";
}
final public function getCreatedAt(): string
{
return $this->created_at ?? "";
}
final public function getDeletedAt(): string
{
return $this->deleted_at ?? "";

View File

@ -8,7 +8,13 @@ class ClientEntity extends CustomerEntity
{
const PK = ClientModel::PK;
const TITLE = ClientModel::TITLE;
protected array $nullableFields = [
'id',
'passwd',
];
protected $attributes = [
'id' => null,
'passwd' => null,
'site' => '',
'name' => '',
'phone' => '',

View File

@ -8,7 +8,12 @@ class ServiceEntity extends CustomerEntity
{
const PK = ServiceModel::PK;
const TITLE = ServiceModel::TITLE;
protected array $nullableFields = [
'serverinfo_uid',
'end_at'
];
protected $attributes = [
'serverinfo_uid' => null,
'code' => '',
'title' => '',
'site' => '',
@ -20,7 +25,7 @@ class ServiceEntity extends CustomerEntity
'sale' => 0,
'amount' => 0,
'start_at' => '',
'end_at' => '',
'end_at' => null,
'status' => '',
'history' => ''
];
@ -28,58 +33,62 @@ class ServiceEntity extends CustomerEntity
{
parent::__construct($data);
}
final public function getUserUid(): int|null
public function getUserUid(): int|null
{
return $this->user_uid ?? null;
}
final public function getClientInfoUid(): int|null
public function getClientInfoUid(): int|null
{
return $this->clientinfo_uid ?? null;
}
final public function getServerInfoUid(): int|null
public function getServerInfoUid(): int|null
{
return $this->serverinfo_uid ?? null;
}
//기본기능용
final public function getCode(): string
public function getCode(): string
{
return $this->code ?? '';
}
final public function getSite(): string
public function getSite(): string
{
return $this->site ?? '';
}
final public function getLocation(): string
public function getLocation(): string
{
return $this->location ?? '';
}
final public function getBillingAt(): string
public function getBillingAt(): string
{
return $this->billing_at ?? '';
}
//청구금액->기본:상면비+회선비+서버금액(price)+서버파트연결(월비용)-할인액
//상면비
final public function getRack(): int
public function getRack(): int
{
return $this->rack ?? 0;
}
//회선비
final public function getLine(): int
public function getLine(): int
{
return $this->line ?? 0;
}
final public function getSale(): int
public function getSale(): int
{
return $this->sale ?? 0;
}
final public function getAmount(): int
public function getAmount(): int
{
return $this->amount ?? 0;
}
final public function getStartAt(): string
public function getStartAt(): string
{
return $this->start_at ?? '';
}
public function getEndAt(): string|null
{
return $this->end_at ?? null;
}
public function getHistory(): string
{
return $this->history ?? '';

View File

@ -8,16 +8,14 @@ class AccountEntity extends WalletEntity
{
const PK = AccountModel::PK;
const TITLE = AccountModel::TITLE;
protected $casts = [
'clientinfo_uid' => 'integer',
'user_uid' => '?integer',
protected array $nullableFields = [
'alias'
];
protected $attributes = [
'clientinfo_uid' => null,
'user_uid' => null,
'title' => '',
'bank' => '',
'alias' => '',
'alias' => null,
'issue_at' => '',
'amount' => 0,
'balance' => 0,

View File

@ -8,14 +8,22 @@ class CHASSISEntity extends EquipmentEntity
{
const PK = CHASSISModel::PK;
const TITLE = CHASSISModel::TITLE;
protected array $nullableFields = [
'cpuinfo_uid',
'raminfo_uid',
'diskinfo_uid',
];
protected $attributes = [
'title' => '',
'price' => 0,
'used' => 0,
'stock' => 0,
'status' => '',
'cpuinfo_uid' => null,
'cpu_cnt' => 0,
'raminfo_uid' => null,
'ram_cnt' => 0,
'diskinfo_uid' => null,
'disk_cnt' => 0,
];
public function __construct(array|null $data = null)

View File

@ -7,13 +7,17 @@ use App\Models\Equipment\LineModel;
class LineEntity extends EquipmentEntity
{
const PK = LineModel::PK;
protected array $nullableFields = [
'start_at',
'end_at',
];
protected $attributes = [
'title' => '',
'type' => '',
'protocol' => '',
'bandwith' => '',
'start_at' => 0,
'end_at' => 0,
'start_at' => null,
'end_at' => null,
'status' => '',
'content' => '',
];
@ -34,13 +38,13 @@ class LineEntity extends EquipmentEntity
{
return $this->bandwith ?? '';
}
public function getStartAt(): string
public function getStartAt(): string|null
{
return $this->start_at ?? '';
return $this->start_at ?? null;
}
public function getEndAt(): string
public function getEndAt(): string|null
{
return $this->end_at ?? '';
return $this->end_at ?? null;
}
public function getContent(): string
{

View File

@ -9,8 +9,18 @@ class ServerEntity extends EquipmentEntity
const PK = ServerModel::PK;
const TITLE = ServerModel::TITLE;
protected array $nullableFields = [
'clientinfo_uid',
'serviceinfo_uid',
'chassisinfo_uid',
'switchinfo_uid',
'ip',
'viewer',
'os',
'format_at',
];
protected $attributes = [
'user_uid' => null,
'clientinfo_uid' => null,
'serviceinfo_uid' => null,
'chassisinfo_uid' => null,
@ -23,7 +33,7 @@ class ServerEntity extends EquipmentEntity
'os' => null,
'price' => 0,
'manufactur_at' => '',
'format_at' => '',
'format_at' => null,
'status' => '',
];
public function __construct(array|null $data = null)
@ -65,21 +75,23 @@ class ServerEntity extends EquipmentEntity
{
return $this->code ?? null;
}
public function getType(): string
public function getType(): ?string
{
return $this->type ?? null;
}
public function getIP(): string
public function getIP(): ?string
{
return $this->ip ?? '';
return $this->ip ?? null;
}
public function getViewer(): string
public function getViewer(): ?string
{
return $this->viewer ?? '';
return $this->viewer ?? null;
}
public function getOS(): string
public function getOS(): ?string
{
return $this->os ?? '';
return $this->os ?? null;
}
public function getPrice(): int
{
@ -89,8 +101,8 @@ class ServerEntity extends EquipmentEntity
{
return $this->manufactur_at ?? '';
}
public function getFormatAt(): string
public function getFormatAt(): ?string
{
return $this->format_at ?? '';
return $this->format_at ?? null;
}
}

View File

@ -8,19 +8,24 @@ class ServerPartEntity extends EquipmentEntity
{
const PK = ServerPartModel::PK;
const TITLE = ServerPartModel::TITLE;
protected array $nullableFields = [
'clientinfo_uid',
'part_uid',
'serviceinfo_uid',
'billing_at',
'extra',
];
protected $attributes = [
'clientinfo_uid' => null,
'part_uid' => null,
'serverinfo_uid' => null,
'serviceinfo_uid' => null,
'title' => '',
'type' => '',
'billing' => '',
'billing_at' => '',
'billing_at' => null,
'cnt' => 0,
'amount' => 0,
'extra' => '',
'extra' => null,
];
public function __construct(array|null $data = null)
{
@ -79,9 +84,9 @@ class ServerPartEntity extends EquipmentEntity
{
return $this->billing ?? "";
}
public function getBillingAt(): string
public function getBillingAt(): string|null
{
return $this->billing_at ?? "";
return $this->billing_at ?? null;
}
public function getCnt(): int
{
@ -91,8 +96,8 @@ class ServerPartEntity extends EquipmentEntity
{
return $this->amount ?? 0;
}
public function getExtra(): string
public function getExtra(): string|null
{
return $this->extra ?? '';
return $this->extra ?? null;
}
}

View File

@ -8,7 +8,11 @@ class MylogEntity extends CommonEntity
{
const PK = Model::PK;
const TITLE = Model::TITLE;
protected array $nullableFields = [
'user_uid',
];
protected $attributes = [
'user_uid' => null,
'title' => '',
'status' => '',
'content' => ''
@ -19,4 +23,8 @@ class MylogEntity extends CommonEntity
}
//공통부분
//Common Function
public function getUserUid(): int|null
{
return $this->user_uid ?? null;
}
}

View File

@ -9,13 +9,20 @@ class PaymentEntity extends CommonEntity
{
const PK = PaymentModel::PK;
const TITLE = PaymentModel::TITLE;
protected array $nullableFields = [
'serviceinfo_uid',
'serverpartinfo_uid',
'pay',
];
protected $attributes = [
'serviceinfo_uid' => null,
'serverpartinfo_uid' => null,
'title' => '',
'amount' => 0,
'billing' => "",
'billing_at' => "",
'billing_month' => 0,
'pay' => "",
'pay' => null,
'status' => '',
'content' => ''
];
@ -23,18 +30,22 @@ class PaymentEntity extends CommonEntity
{
parent::__construct($data);
}
final public function getUserUid(): int|null
public function getUserUid(): int|null
{
return $this->user_uid ?? null;
}
final public function getClientInfoUid(): int|null
public function getClientInfoUid(): int|null
{
return $this->clientinfo_uid ?? null;
}
final public function getServiceInfoUid(): int|null
public function getServiceInfoUid(): int|null
{
return $this->serviceinfo_uid ?? null;
}
public function getServerPartInfoUid(): int|null
{
return $this->serverpartinfo_uid ?? null;
}
//기본기능
public function getCustomTitle(): string
{
@ -56,9 +67,9 @@ class PaymentEntity extends CommonEntity
{
return $this->billing_month ?? 0;
}
public function getPay(): string
public function getPay(): int|null
{
return $this->pay ?? "";
return $this->pay ?? null;
}
public function getContent(): string
{

View File

@ -9,18 +9,15 @@ class UserEntity extends CommonEntity
{
const PK = Model::PK;
const TITLE = Model::TITLE;
/**
* @var array DB 컬럼 타입이 VARCHAR(255)이고 CSV 형식으로 통일하기 위해 json-array 캐스팅을 제거합니다.
*/
protected $casts = [
// 'role' => 'json-array', // 🚫 CSV 형식 저장을 위해 제거
protected array $nullableFields = [
'mobile',
];
protected $attributes = [
'id' => '',
'passwd' => '',
'name' => "",
'email' => "",
'mobile' => '',
'mobile' => null,
'role' => "",
'status' => '',
];

View File

@ -12,6 +12,10 @@ class BoardModel extends CommonModel
protected $table = self::TABLE;
protected $primaryKey = self::PK;
protected $returnType = BoardEntity::class;
protected array $nullableFields = [
'user_uid',
'worker_uid',
];
protected $allowedFields = [
"uid",
"user_uid",

View File

@ -43,14 +43,16 @@ abstract class CommonModel extends Model
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = []; //Field 값이 NULL일 경우 DB Default값 적용용
protected $beforeInsert = ['emptyStringToNull']; //Field 값이 NULL일 경우 DB Default값 적용용
protected $afterInsert = [];
protected $beforeUpdate = []; //Field 값이 NULL일 경우 DB Default값 적용용
protected $beforeUpdate = ['emptyStringToNull']; //Field 값이 NULL일 경우 DB Default값 적용용
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
protected array $nullableFields = []; // 모델별로 override
protected function __construct()
{
parent::__construct();
@ -75,4 +77,35 @@ abstract class CommonModel extends Model
{
return $this->allowedFields;
}
protected function emptyStringToNull(array $data): array
{
if (!isset($data['data']) || !is_array($data['data'])) {
return $data;
}
// 공통 모델에서는 아무 필드도 강제하지 않음 (안전)
if (empty($this->nullableFields)) {
return $data;
}
foreach ($this->nullableFields as $field) {
if (array_key_exists($field, $data['data'])) {
$v = $data['data'][$field];
// 문자열이면 trim 후, 빈문자면 null
if (is_string($v)) {
$v = trim($v);
$data['data'][$field] = ($v === '') ? null : $v;
} else {
// 문자열이 아닌데도 '' 같은 케이스 방어 (거의 없음)
if ($v === '')
$data['data'][$field] = null;
}
}
}
return $data;
}
}

View File

@ -13,6 +13,10 @@ class ClientModel extends CustomerModel
// protected $useAutoIncrement = false;
protected $primaryKey = self::PK;
protected $returnType = ClientEntity::class;
protected array $nullableFields = [
'id',
'passwd',
];
protected $allowedFields = [
"uid",
"user_uid",

View File

@ -14,6 +14,10 @@ class ServiceModel extends CustomerModel
// protected $useAutoIncrement = false;
protected $primaryKey = self::PK;
protected $returnType = ServiceEntity::class;
protected array $nullableFields = [
'serverinfo_uid',
'end_at'
];
protected $allowedFields = [
"uid",
"user_uid",

View File

@ -12,6 +12,9 @@ class AccountModel extends WalletModel
protected $table = self::TABLE;
protected $primaryKey = self::PK;
protected $returnType = AccountEntity::class;
protected array $nullableFields = [
'alias'
];
protected $allowedFields = [
"uid",
"user_uid",

View File

@ -13,6 +13,11 @@ class CHASSISModel extends EquipmentModel
protected $primaryKey = self::PK;
// protected $useAutoIncrement = false;
protected $returnType = CHASSISEntity::class;
protected array $nullableFields = [
'cpuinfo_uid',
'raminfo_uid',
'diskinfo_uid',
];
protected $allowedFields = [
"uid",
"title",

View File

@ -12,6 +12,10 @@ class LineModel extends EquipmentModel
protected $table = self::TABLE;
protected $primaryKey = self::PK;
protected $returnType = LineEntity::class;
protected array $nullableFields = [
'start_at',
'end_at',
];
protected $allowedFields = [
"uid",
"type",

View File

@ -13,6 +13,16 @@ class ServerModel extends EquipmentModel
// protected $useAutoIncrement = false;
protected $primaryKey = self::PK;
protected $returnType = ServerEntity::class;
protected array $nullableFields = [
'clientinfo_uid',
'serviceinfo_uid',
'chassisinfo_uid',
'switchinfo_uid',
'ip',
'viewer',
'os',
'format_at',
];
protected $allowedFields = [
"uid",
"user_uid",

View File

@ -12,6 +12,13 @@ class ServerPartModel extends EquipmentModel
protected $table = self::TABLE;
protected $primaryKey = self::PK;
protected $returnType = ServerPartEntity::class;
protected array $nullableFields = [
'clientinfo_uid',
'part_uid',
'serviceinfo_uid',
'billing_at',
'extra',
];
protected $allowedFields = [
"uid",
"part_uid",

View File

@ -12,6 +12,9 @@ class MylogModel extends CommonModel
protected $table = self::TABLE;
protected $primaryKey = self::PK;
protected $returnType = MylogEntity::class;
protected array $nullableFields = [
'user_uid',
];
protected $allowedFields = [
"uid",
"user_uid",

View File

@ -12,6 +12,11 @@ class PaymentModel extends CommonModel
protected $table = self::TABLE;
protected $primaryKey = self::PK;
protected $returnType = PaymentEntity::class;
protected array $nullableFields = [
'serviceinfo_uid',
'serverpartinfo_uid',
'pay',
];
protected $allowedFields = [
"uid",
"user_uid",

View File

@ -12,6 +12,9 @@ class UserModel extends CommonModel
protected $table = self::TABLE;
protected $primaryKey = self::PK;
protected $returnType = UserEntity::class;
protected array $nullableFields = [
'mobile',
];
protected $allowedFields = [
"uid",
"id",

View File

@ -238,6 +238,7 @@ abstract class CommonService
try {
// INSERT 시 Entity의 PK는 0 또는 NULL이어야 함 (DB가 ID를 생성하도록)
$initialPK = $entity->getPK();
// dd($entity);
$result = $this->model->save($entity);
// 최종적으로 DB에 반영된 PK를 반환받습니다. (UPDATE이면 기존 PK, INSERT이면 새 PK)
$entity->{$this->getPKField()} = $this->handle_save_result($result, $initialPK);

View File

@ -62,9 +62,6 @@ class ClientService extends CustomerService
)));
$formDatas[$field] = $value;
break;
case "format_at":
$formDatas[$field] = $value === '' ? null : $value;
break;
default:
$formDatas = parent::action_process_fieldhook($field, $value, $formDatas);
break;

View File

@ -120,21 +120,33 @@ class ServerService extends EquipmentService
{
return $entity;
}
protected function action_process_fieldhook(string $field, $value, array $formDatas): array
{
switch ($field) {
default:
$formDatas = parent::action_process_fieldhook($field, $value, $formDatas);
break;
}
return $formDatas;
}
protected function create_process(array $formDatas): ServerEntity
{
$entity = parent::create_process($formDatas);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
}
//서버추가시 서버파트 자동추가용
// dd($entity);
//새로운 IP 추가 (IP가 정의 되어 있으면)
if ($entity->getIP()) {
service('part_ipservice')->attachToServer($entity);
}
//새로운 Switch 추가 (Switch가 정의 되어 있으면)
if ($entity->getSwitchInfoUid()) {
service('part_switchservice')->attachToServer($entity);
}
//새로운 Chassis 추가 (Chassis가 정의 되어 있으면)
service('equipment_chassisservice')->attachToServer($entity);
//새로운 ServerPart 추가 (ServerPart가 정의 되어 있으면)
service('equipment_serverpartservice')->attachToServer($entity);
return $entity;
}
@ -150,22 +162,32 @@ class ServerService extends EquipmentService
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
}
//서버정보변경시 서버파트 정보변경용
//IP변경
if ($oldEntity->getIP() !== $entity->getIP()) {
//기존 IP 제거
service('part_ipservice')->detachFromServer($oldEntity);
//새로운 IP 추가 (IP가 정의 되어 있으면)
if ($entity->getIP()) {
service('part_ipservice')->attachToServer($entity);
}
}
//SWITCH변경
if ($oldEntity->getSwitchInfoUid() !== $entity->getSwitchInfoUid()) {
service('part_switchservice')->detachFromServer($oldEntity);
//새로운 Switch 추가 (Switch가 정의 되어 있으면)
if ($entity->getSwitchInfoUid()) {
service('part_switchservice')->attachToServer($entity);
}
}
//샷시변경
if ($oldEntity->getSwitchInfoUid() !== $entity->getSwitchInfoUid()) {
if ($oldEntity->getChassisInfoUid() !== $entity->getChassisInfoUid()) {
service('equipment_chassisservice')->detachFromServer($oldEntity);
//새로운 Chassis 추가 (Chassis가 정의 되어 있으면)
if ($entity->getChassisInfoUid()) {
service('equipment_chassisservice')->attachToServer($entity);
}
}
//서비스변경
if ($entity->getServiceInfoUid() !== null) { //서비스가 정의 되어 있으면
$serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid());
if (!$serviceEntity instanceof ServiceEntity) {