78 lines
2.0 KiB
PHP
78 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Entities;
|
|
|
|
use CodeIgniter\Entity\Entity;
|
|
|
|
abstract class CommonEntity extends Entity
|
|
{
|
|
protected $datamap = [];
|
|
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
|
|
|
|
/**
|
|
* 이 엔티티에서 "빈문자/공백 입력은 NULL로 저장"해야 하는 필드 목록.
|
|
* 기본은 빈 배열이고, 각 Entity에서 필요한 것만 override해서 채우면 됨.
|
|
*/
|
|
public function __construct(array|null $data = null)
|
|
{
|
|
parent::__construct($data);
|
|
}
|
|
|
|
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)) {
|
|
return $this->attributes[$key] = $value;
|
|
}
|
|
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->attributes['status'] ?? "";
|
|
}
|
|
|
|
final public function getUpdatedAt(): string
|
|
{
|
|
return $this->attributes['updated_at'] ?? "";
|
|
}
|
|
|
|
final public function getCreatedAt(): string
|
|
{
|
|
return $this->attributes['created_at'] ?? "";
|
|
}
|
|
|
|
final public function getDeletedAt(): string
|
|
{
|
|
return $this->attributes['deleted_at'] ?? "";
|
|
}
|
|
}
|