servermgrv2/app/Models/UserSNSModel.php
최준흠git config git config --helpgit config --global user.name 최준흠 bbd2c11f28 servermgrv2 init...1
2023-07-17 23:24:40 +09:00

65 lines
2.2 KiB
PHP

<?php
namespace App\Models;
use App\Entities\UserSNSEntity;
class UserSNSModel extends CommonModel
{
protected $table = 'user_sns';
// protected $primaryKey = 'uid';
protected $useAutoIncrement = false;
protected $allowedFields = ['uid', 'user_uid', 'site', 'name', 'email', 'status', 'updated_at', 'created_at'];
protected $validationRules = [
'uid' => 'if_exist|min_length[4]|max_length[250]',
'user_uid' => 'if_exist|numeric',
'site' => 'if_exist|min_length[4]',
'name' => 'if_exist|min_length[2]|max_length[20]',
'email' => 'if_exist|valid_email',
'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): ?UserSNSEntity
{
return $this->asObject(UserSNSEntity::class)->where($field, $value)->first();
}
public function getEntity(int $uid): ?UserSNSEntity
{
return $this->getEntityByField($this->primaryKey, $uid);
}
public function create(array $datas): UserSNSEntity
{
$entity = new UserSNSEntity();
$entity->uid = $datas['id'];
$entity->site = $this->getSiteName();
$entity->name = $datas['name'];
$entity->email = $datas['email'];
$entity->status = "standby";
return $this->create_process($entity);
}
public function modify(UserSNSEntity $entity, array $datas): UserSNSEntity
{
foreach ($datas as $field => $value) {
if ($entity->$field != $datas[$field]) {
$entity->$field = $value;
}
}
return $this->modify_process($entity);
}
//Index관련
public function setIndexWordFilter(string $word)
{
parent::setIndexWordFilter($word);
$this->orLike('name', $word, 'both');
$this->orLike('email', $word, 'both'); //befor , after , both
}
public function setIndexOrderBy($field, $order = 'ASC')
{
$this->orderBy("name", "ASC");
parent::setIndexOrderBy($field, $order);
}
}