86 lines
2.6 KiB
PHP
86 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\UserEntity;
|
|
use App\Models\CommonModel;
|
|
|
|
class UserModel extends CommonModel
|
|
{
|
|
const TABLE = "user";
|
|
const PK = "uid";
|
|
const TITLE = "name";
|
|
protected $table = self::TABLE;
|
|
protected $primaryKey = self::PK;
|
|
protected $returnType = UserEntity::class;
|
|
protected $allowedFields = [
|
|
"id",
|
|
"passwd",
|
|
"name",
|
|
"email",
|
|
"mobild",
|
|
"role",
|
|
"status"
|
|
];
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return self::TITLE;
|
|
}
|
|
public function getFieldRule(string $action, string $field, array $rules): array
|
|
{
|
|
if (is_array($field)) {
|
|
throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
|
|
}
|
|
switch ($field) {
|
|
case "id":
|
|
$rules[$field] = "required|trim|min_length[4]|max_length[20]|is_unique[{$this->table}.{$field}]";
|
|
$rules[$field] .= $action == "create" ? "|is_unique[{$this->table}.{$field}]" : "";
|
|
break;
|
|
case "email":
|
|
$rules[$field] = "if_exist|trim|valid_email";
|
|
break;
|
|
case "role":
|
|
//아래 Rule은 입력시에는 되는데 수정시에는 않됨 이유를 ?
|
|
// $rules[$field] = "required|in_list[master,director,cloudflare,manager,gold,silver,brone,vip,user]";
|
|
//아래 Rule은 checkbox를 사용시에는 required만 우선 써야 수정시 validate문제없음
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "role[]":
|
|
case "roles[]":
|
|
$rules[$field] = "if_exist|trim|string";
|
|
break;
|
|
case "passwd":
|
|
default:
|
|
$rules = parent::getFieldRule($action, $field, $rules);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
|
|
public function getEntityByPK(int $uid): null|UserEntity
|
|
{
|
|
$this->where($this->getPKField(), $uid);
|
|
return $this->getEntity();
|
|
}
|
|
public function getEntityByID(string $id): null|UserEntity
|
|
{
|
|
$this->where('id', $id);
|
|
return $this->getEntity();
|
|
}
|
|
|
|
//create용
|
|
public function create(array $formDatas = []): UserEntity
|
|
{
|
|
return $this->create_process(new UserEntity(), $formDatas);
|
|
}
|
|
//modify용
|
|
public function modify(UserEntity $entity, array $formDatas): UserEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
}
|