Automation/app/Models/CommonModel.php
2024-09-05 20:13:31 +09:00

142 lines
5.1 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
abstract class CommonModel extends Model
{
protected $table = '';
protected $primaryKey = '';
protected $useAutoIncrement = true;
protected $returnType = 'array';
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() {}
abstract public function getPK(): string;
final public function getEntity()
{
return $this->asObject($this->returnType)->first();
}
final protected function setEntity($entity)
{
if ($entity->hasChanged()) {
if (!$this->save($entity)) {
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->errors(), true));
}
}
return $entity;;
}
final public function getEntitys(): array
{
return $this->asObject($this->returnType)->findAll();
}
final protected function save_process($entity)
{
// echo var_export($entity, true);
// exit;
if ($entity->hasChanged()) {
if (!$this->save($entity)) {
log_message("error", __FUNCTION__ . "에서 호출:" . $this->getLastQuery());
log_message("error", implode("\n", $this->errors()));
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . $this->getLastQuery() . "\n" . var_dump($this->errors(), true));
}
} else {
throw new \Exception(__FUNCTION__ . " 오류 발생.\n 기존정보와 동일하여 수정되지 않았습니다.");
}
return $entity;
}
//create , modify 직전 작업용 작업
protected function changeFormData(string $action, string $field, array $formDatas, $entity)
{
switch ($field) {
// case "user_uid": //입력데이터로 있을시 관리툴에서 (사용자,등)추가, 없을시는 입력의 경우에만 자동(장바구니,등)으로 추가
// if (array_key_exists($field, $formDatas) && !is_null($formDatas[$field])) {
// //관리툴 USERSNS에서 사용자 연동 시 추가기능등에 사용
// $entity->$field = $formDatas[$field];
// } elseif ($action == 'create' && $this->_session->get(SESSION_NAMES["ISLOGIN"])) {
// //Front에서 장바구니,게시판등에 추가시 로그온한경우 자동 추가기능등에 사용
// $auth = $this->_session->get(SESSION_NAMES["AUTH"]);
// $entity->$field = $auth[AUTH_FIELDS["ID"]];
// }
// break;
case "passwd":
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
$entity->$field = password_hash($formDatas[$field], PASSWORD_DEFAULT);
}
break;
case "content":
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
$entity->$field = htmlentities($formDatas[$field]);
}
break;
default:
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
$entity->$field = $formDatas[$field];
}
break;
}
return $entity;
}
protected function create_process($entity, array $formDatas)
{
foreach ($this->allowedFields as $field) {
$entity = $this->changeFormData('create', $field, $formDatas, $entity);
}
$entity = $this->save_process($entity);
//primaryKey가 자동입력이면
if ($this->useAutoIncrement) {
$pk = $this->getPK();
$entity->$pk = $this->insertID();
}
return $entity;
}
protected function modify_process($entity, array $formDatas)
{
$entity->updated_at = time(); //수정한 시간정의
foreach ($this->allowedFields as $field) {
$entity = $this->changeFormData('modify', $field, $formDatas, $entity);
}
return $this->save_process($entity);
}
}