98 lines
3.1 KiB
PHP
98 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\BoardEntity;
|
|
|
|
class BoardModel extends BaseHierarchyModel
|
|
{
|
|
protected $table = "tw_board";
|
|
protected $returnType = BoardEntity::class;
|
|
public function __construct()
|
|
{
|
|
parent::__construct('Board');
|
|
$this->allowedFields = [
|
|
...$this->allowedFields,
|
|
"category_uid",
|
|
"user_uid", 'title', "content",
|
|
"passwd", "board_file", "view_cnt", "status"
|
|
];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return 'title';
|
|
}
|
|
public function getContentField(): string
|
|
{
|
|
return 'content';
|
|
}
|
|
public function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case "category_uid":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
case "user_uid":
|
|
$rules[$field] = $this->getUUIDFieldRule('required');
|
|
break;
|
|
case $this->getTitleField():
|
|
case "content":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
case "board_file": //uploaded[{$field}] == requried와 같은의미
|
|
$rules[$field] = "if_exist|string";
|
|
break;
|
|
case "view_cnt":
|
|
$rules[$field] = "if_exist|numeric";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getEntity($conditions): BoardEntity
|
|
{
|
|
return $this->where($conditions)->first() ?: throw new \Exception("해당 데이터가 없습니다.\n" . var_export($conditions, true));
|
|
}
|
|
|
|
public function create(array $formDatas): BoardEntity
|
|
{
|
|
return $this->create_process(new BoardEntity(), $formDatas);
|
|
}
|
|
public function modify(BoardEntity $entity, array $formDatas): BoardEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
public function reply($parent_entity, array $formDatas): BoardEntity
|
|
{
|
|
return $this->reply_process($parent_entity, new BoardEntity(), $formDatas);
|
|
}
|
|
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
if ($word !== DEFAULTS['EMPTY']) {
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike($this->getTitleField(), $word, "both");
|
|
$this->orLike("content", $word, "both"); //befor , after , both
|
|
}
|
|
}
|
|
|
|
public function setIndexOrderBy(?string $field, ?string $order)
|
|
{
|
|
//계단식의 경우는 먼저 다른것보다 먼저 orderBy가 수행되어야 하므로
|
|
$this->orderBy("grpno", "DESC");
|
|
$this->orderBy("grporder", "DESC");
|
|
parent::setIndexOrderBy($field, $order);
|
|
}
|
|
|
|
//조회수 올리기
|
|
final public function addViewCount(BoardEntity $entity, int $view_cnt = 1): BoardEntity
|
|
{
|
|
$entity->view_cnt += $view_cnt;
|
|
return $this->save_process($entity);
|
|
}
|
|
}
|