92 lines
3.0 KiB
PHP
92 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\UserSNSEntity;
|
|
|
|
class UserSNSModel extends BaseModel
|
|
{
|
|
protected $table = "tw_user_sns";
|
|
protected $returnType = UserSNSEntity::class;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->allowedFields = [...$this->allowedFields, ...$this->getFields(), "user_uid"];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return 'name';
|
|
}
|
|
public function getFields(string $action = ""): array
|
|
{
|
|
$fields = ["site", "id", $this->getTitleField(), "email", "detail", "status"];
|
|
switch ($action) {
|
|
case "index":
|
|
case "excel":
|
|
return ["user_uid", "site", "id", $this->getTitleField(), "email", "status", "created_at"];
|
|
break;
|
|
case "view":
|
|
return [...$fields, "updated_at", "created_at"];
|
|
break;
|
|
default:
|
|
return $fields;
|
|
break;
|
|
}
|
|
}
|
|
public function getFieldFilters(): array
|
|
{
|
|
return ["user_uid", "status"];
|
|
}
|
|
public function getFieldBatchFilters(): array
|
|
{
|
|
return parent::getFieldBatchFilters();
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case "id":
|
|
$rules[$field] = "required|trim|string";
|
|
$rules[$field] .= $action == "insert" ? "|is_unique[{$this->table}.{$field}]" : "";
|
|
break;
|
|
case $this->getTitleField():
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "email":
|
|
$rules[$field] = "required|valid_email";
|
|
break;
|
|
case "detail":
|
|
$rules[$field] = "if_exist|string";
|
|
break;
|
|
case "status":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getEntity($conditions): UserSNSEntity
|
|
{
|
|
return $this->where($conditions)->first() ?: throw new \Exception("해당 데이터가 없습니다.\n" . var_export($conditions, true));
|
|
}
|
|
|
|
public function create(array $formDatas): UserSNSEntity
|
|
{
|
|
return $this->create_process(new UserSNSEntity(), $formDatas);
|
|
}
|
|
public function modify(UserSNSEntity $entity, array $formDatas): UserSNSEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike($this->getTitleField(), $word, "both");
|
|
$this->orLike("email", $word, "both"); //befor , after , both
|
|
}
|
|
}
|