65 lines
2.3 KiB
PHP
65 lines
2.3 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', 'status', 'updated_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]',
|
|
'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
|
|
{
|
|
$entity = new UserEntity();
|
|
foreach ($datas as $field => $value) {
|
|
$entity->$field = $field === 'passwd' ? $entity->getEncryptedPassword($value) : $value;
|
|
}
|
|
return parent::create_process($entity);
|
|
}
|
|
public function modify(UserEntity $entity, array $datas): UserEntity
|
|
{
|
|
foreach ($datas as $field => $value) {
|
|
if ($entity->$field != $datas[$field]) {
|
|
$entity->$field = $field === 'passwd' ? $entity->getEncryptedPassword($value) : $value;
|
|
}
|
|
}
|
|
return parent::modify_process($entity);
|
|
}
|
|
|
|
//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);
|
|
}
|
|
}
|