servermgrv2/app/Models/UserModel.php
2023-07-17 21:09:49 +09:00

57 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use App\Entities\UserEntity;
class UserModel extends CommonModel
{
protected $table = 'user';
// protected $primaryKey = 'uid';
// protected $useAutoIncrement = true;
protected $allowedFields = ['id', 'passwd', 'name', 'email', 'role', 'oauth_id', 'status', 'updated_at', 'created_at'];
protected $validationRules = [
'uid' => 'if_exist|numeric',
'id' => 'if_exist|min_length[4]|max_length[20]',
'passwd' => 'if_exist|trim|min_length[4]|max_length[150]',
'confirmpassword' => 'if_exist|trim|matches[passwd]',
'name' => 'if_exist|min_length[2]|max_length[20]',
'email' => 'if_exist|valid_email',
'role' => 'if_exist|in_list[user,manager,cloudflare,director,master]',
'oauth_id' => 'if_exist|trim|min_length[4]',
'status' => 'if_exist|in_list[use,unuse,standby]',
'updated_at' => 'if_exist|valid_date',
'created_at' => 'if_exist|valid_date',
];
public function getEntityByField($field, $value): ?UserEntity
{
return $this->asObject(UserEntity::class)->where($field, $value)->first();
}
public function getEntity(int $uid): ?UserEntity
{
return $this->getEntityByField($this->primaryKey, $uid);
}
public function create(array $datas): UserEntity
{
return $this->create_CommonTrait(new UserEntity($datas), $datas);
}
public function modify(UserEntity $entity, array $datas): UserEntity
{
return $this->modify_CommonTrait($entity, $datas);
}
//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);
}
}