servermgrv2/app/Models/UserModel.php
최준흠git config git config --helpgit config --global user.name 최준흠 eaa3a76677 servermgrv2 init...
2023-07-23 23:26:19 +09:00

93 lines
3.0 KiB
PHP

<?php
namespace App\Models;
use App\Entities\UserEntity;
class UserModel extends BaseModel
{
public function __construct()
{
parent::__construct();
$this->table = 'tw_user';
$this->useAutoIncrement = false;
$this->allowedFields = [
...$this->allowedFields,
'uid',
...$this->getFields(),
];
$this->validationRules = [
...$this->validationRules,
...$this->getFieldRules($this->getFields()),
];
}
public function getFields(array $fields = array(), array $skips = array()): array
{
$fields = ['id', 'email', 'passwd', 'name', 'role', 'status', ...$fields];
return parent::getFields($fields, $skips);
}
public function getFieldFilters(array $fields = array(), array $skips = array()): array
{
$fields = ['user_uid', 'status', ...$fields];
return parent::getFieldFilters($fields, $skips);
}
protected function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case 'uid':
$rules[$field] = 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]';
break;
case 'id':
$rules[$field] = 'required|trim|min_length[4]|max_length[20]';
break;
case 'passwd':
$rules[$field] = 'required|string';
$rules['confirmpassword'] = 'if_exist|string|matches[passwd]';
break;
case 'name':
$rules[$field] = 'required|trim|string';
break;
case 'email':
$rules[$field] = 'required|valid_email';
break;
case 'role':
$rules[$field] = 'required|string';
break;
default:
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;
}
public function getEntity($where): UserEntity
{
$entity = $this->asObject(UserEntity::class)->where($where)->first();
return $entity ?: throw new \Exception("{$where}의 해당 사용자가 없습니다.\n ");
}
public function getEntitys($where): array
{
return $this->asObject(UserEntity::class)->where($where)->findAll();
}
public function create(array $formDatas): UserEntity
{
return $this->create_process(new UserEntity(), $formDatas);
}
public function modify(UserEntity $entity, array $formDatas): UserEntity
{
return $this->modify_process($entity, $formDatas);
}
//Index관련
public function setIndexWordFilter(string $word)
{
parent::setIndexWordFilter($word);
$this->orLike('id', $word, 'both');
$this->orLike('name', $word, 'both'); //befor , after , both
}
public function setIndexOrderBy($field, $order = 'ASC')
{
$this->orderBy("name", "ASC");
parent::setIndexOrderBy($field, $order);
}
}