168 lines
5.4 KiB
PHP
168 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
abstract class CommonModel extends Model
|
|
{
|
|
protected $table = '';
|
|
protected $primaryKey = '';
|
|
protected $useAutoIncrement = true;
|
|
// protected $returnType = 'array';
|
|
//true이면 모든 delete * 메소드 호출은 실제로 행을 삭제하는 것이 아니라 플래그를 데이터베이스로 설정
|
|
protected $useSoftDeletes = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [];
|
|
|
|
protected bool $allowEmptyInserts = false;
|
|
protected bool $updateOnlyChanged = true;
|
|
|
|
protected array $casts = [];
|
|
protected array $castHandlers = [];
|
|
|
|
// Dates
|
|
protected $useTimestamps = false;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
|
|
// Validation
|
|
protected $validationRules = [];
|
|
protected $validationMessages = [];
|
|
protected $skipValidation = false;
|
|
protected $cleanValidationRules = true;
|
|
|
|
// Callbacks
|
|
protected $allowCallbacks = true;
|
|
protected $beforeInsert = [];
|
|
protected $afterInsert = [];
|
|
protected $beforeUpdate = [];
|
|
protected $afterUpdate = [];
|
|
protected $beforeFind = [];
|
|
protected $afterFind = [];
|
|
protected $beforeDelete = [];
|
|
protected $afterDelete = [];
|
|
protected function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
final public function getTable(): string
|
|
{
|
|
return constant("static::TABLE");
|
|
}
|
|
final public function getPKField(): string
|
|
{
|
|
return constant("static::PK");
|
|
}
|
|
final public function getTitleField(): string
|
|
{
|
|
return constant("static::TITLE");
|
|
}
|
|
final public function useAutoIncrement(): bool
|
|
{
|
|
return $this->useAutoIncrement;
|
|
}
|
|
/**
|
|
* Entity 삽입을 처리하고, $useAutoIncrement가 false일 경우 UUID를 자동으로 생성합니다.
|
|
* @param object|array $data Entity 객체 또는 데이터 배열
|
|
* @param bool $returnID 삽입 후 ID를 반환할지 여부 (자동 증가 키일 경우 true)
|
|
* @return bool|int|string
|
|
*/
|
|
public function insert($data = null, bool $returnID = true)
|
|
{
|
|
// 1. 공통 전처리 로직 (예: 모든 모델에 대한 로그 기록, 공통 필드 설정 등)
|
|
$data = $this->preProcessData($data);
|
|
|
|
// 2. PK 자동 할당 로직
|
|
if (!$this->useAutoIncrement) {
|
|
$data = $this->ensurePrimaryKey($data);
|
|
}
|
|
|
|
// 3. BaseModel의 insert 호출
|
|
// 문자열 PK일 경우, $returnID를 false로 넘겨서 ID 반환을 강제하지 않는 것이 좋습니다.
|
|
$baseReturn = parent::insert($data, $this->useAutoIncrement);
|
|
|
|
// 4. 후처리 및 반환
|
|
if ($this->useAutoIncrement) {
|
|
// 정수 PK: BaseModel이 반환한 새로운 ID를 그대로 반환
|
|
return $baseReturn;
|
|
} else {
|
|
// 문자열 PK: BaseModel의 insert는 성공 시 true/1을 반환. true로 통일하여 반환
|
|
return $baseReturn !== false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Entity나 데이터 배열을 받아서 필요하다면 Primary Key를 생성하여 할당합니다.
|
|
* @param object|array $data
|
|
* @return object|array
|
|
*/
|
|
protected function ensurePrimaryKey($data)
|
|
{
|
|
$pkName = $this->primaryKey;
|
|
$entityData = is_object($data) ? $data->attributes : (array) $data;
|
|
|
|
// PK가 설정되어 있지 않은 경우
|
|
if (empty($entityData[$pkName])) {
|
|
$newUuid = service('uuid')->uuid4()->toString();
|
|
|
|
if (is_object($data)) {
|
|
// Entity 객체일 경우 __set()을 통해 Setter를 호출하며 할당
|
|
$data->$pkName = $newUuid;
|
|
} else {
|
|
// 배열일 경우 배열에 할당
|
|
$data[$pkName] = $newUuid;
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 모든 삽입/수정 작업 전 공통 로직을 처리하는 확장 지점.
|
|
*/
|
|
protected function preProcessData($data)
|
|
{
|
|
// 예: $data['data']['updated_by'] = session('user_id');
|
|
return $data;
|
|
}
|
|
|
|
// ******************************************************
|
|
// 2. UPDATE 및 DELETE 확장
|
|
// ******************************************************
|
|
|
|
/**
|
|
* BaseModel의 update 메서드를 호출하기 전/후에 공통 로직을 적용하는 확장 지점.
|
|
* @param array|int|string|null $id
|
|
* @param array|object|null $data
|
|
* @return bool
|
|
*/
|
|
public function update($id = null, $data = null): bool
|
|
{
|
|
// 1. 업데이트 전 공통 로직 (preUpdate 이벤트)
|
|
$data = $this->preProcessData($data);
|
|
|
|
// 2. BaseModel의 update 호출
|
|
return parent::update($id, $data);
|
|
|
|
// 3. 업데이트 후 공통 로직 (postUpdate 이벤트)
|
|
}
|
|
|
|
/**
|
|
* BaseModel의 delete 메서드를 호출하기 전/후에 공통 로직을 적용하는 확장 지점.
|
|
* @param array|int|string|null $id
|
|
* @param bool $purge
|
|
* @return bool
|
|
*/
|
|
public function delete($id = null, bool $purge = false): bool
|
|
{
|
|
// 1. 삭제 전 공통 로직 (preDelete 이벤트)
|
|
|
|
// 2. BaseModel의 delete 호출
|
|
return parent::delete($id, $purge);
|
|
|
|
// 3. 삭제 후 공통 로직 (postDelete 이벤트)
|
|
}
|
|
}
|