Automation/app/Models/UserModel.php
2024-08-30 19:32:21 +09:00

67 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use App\Entities\UserEntity;
class UserModel extends CommonModel
{
protected $table = 'mb_users';
protected $primaryKey = 'pid';
protected $useAutoIncrement = true;
protected $returnType = UserEntity::class;
protected $allowedFields = ['pid', 'user_id', 'passwd', 'user_name', 'user_email', 'user_state', 'user_level', 'user_point'];
// Validation
// protected $validationRules = [];
protected $validationRules = [
'pid' => 'if_exist|numeric',
'user_id' => 'if_exist|trim|string',
'passwd' => 'if_exist|trim|string',
// 'confirmpassword' => 'if_exist|trim|matches[passwd]',
'user_name' => 'if_exist|trim|string',
'user_state' => 'if_exist|trim|string',
'user_email' => 'if_exist|trim|valid_email',
'user_level' => 'if_exist|numeric',
'user_point' => 'if_exist|numeric',
// 'proxied' => 'if_exist|in_list[on,off]',
// 'fixed' => 'if_exist|in_list[on,off]',
// 'locked' => 'if_exist|in_list[on,off]',
// 'updated_at' => 'if_exist|valid_date',
// 'created_at' => 'if_exist|valid_date',
];
public function getEntityByPK(int $uid): null|UserEntity
{
$this->where($this->primaryKey, $uid);
return $this->getEntity();
}
public function getEntityByID(string $id): null|UserEntity
{
$this->where('user_id', $id);
return $this->getEntity();
}
public function setPoint(UserEntity $entity, int $point): UserEntity
{
if ($entity->getPoint() != $point) {
$old_point = $entity->getPoint();
$entity->setPoint($point);
$entity = $this->setEntity($entity);
log_message("notice", __FUNCTION__ . "=>{$entity}의 Point가 {$old_point}에서 {$entity->getPoint()}로 변경되었습니다.");
}
return $entity;
}
final public function setLevel(UserEntity $entity, int $level): UserEntity
{
if ($entity->getLevel() != $level) {
$old_level = $entity->getLevel();
$entity->setLevel($level);
$entity = $this->setEntity($entity);
log_message("notice", __FUNCTION__ . "=>{$entity}의 Level이 {$old_level}에서 {$entity->getLevel()}로 변경되었습니다.");
}
return $entity;
}
}