servermgrv2/app/Models/BoardModel.php
최준흠git config git config --helpgit config --global user.name 최준흠 b3659ee820 servermgrv2 init...
2023-07-22 23:19:44 +09:00

79 lines
3.0 KiB
PHP

<?php
namespace App\Models;
use App\Entities\BoardEntity;
class BoardModel extends BaseModel
{
protected $table = 'tw_board';
protected $primaryKey = 'uid';
protected $useAutoIncrement = true;
protected $allowedFields = ['grpno', 'grporder', 'grpdepth', 'board_config_uid', 'user_uid', 'title', 'content', 'passwd', 'view_cnt', 'status', 'updated_at'];
protected $validationRules = [
'grpno' => 'if_exist|numeric',
'grporder' => 'if_exist|numeric',
'grpdepth' => 'if_exist|numeric',
'board_config_uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]',
'user_uid' => 'if_exist|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]',
'title' => 'required|string',
'content' => 'required|string',
'passwd' => 'if_exist|trim|string',
'view_cnt' => 'if_exist|numeric',
'status' => 'if_exist|string',
'updated_at' => 'if_exist|valid_date',
'created_at' => 'if_exist|valid_date',
];
public function getEntityByField($field, $value): ?BoardEntity
{
$entity = $this->asObject(BoardEntity::class)->where($field, $value)->first();
if (is_null($entity)) {
throw new \Exception("해당 데이터가 없습니다.\n {$field}->{$value}");
}
return $entity;
}
public function getEntity($uid): ?BoardEntity
{
return $this->getEntityByField($this->primaryKey, $uid);
}
public function getFieldFormOptions(array $wheres = array(), $temps = array()): array
{
foreach ($this->asObject(BoardEntity::class)->where($wheres)->findAll() as $entity) {
$temps[$entity->getPrimaryKey()] = $entity->getTitle();
}
return $temps;
}
public function create(array $formDatas): BoardEntity
{
//계층형 Create
$entity = $this->create_process(new BoardEntity(), $formDatas);
return $this->create_hierarchy($entity);
}
public function modify(BoardEntity $entity, array $formDatas): BoardEntity
{
return $this->modify_process($entity, $formDatas);
}
public function reply(BoardEntity $parent_entity, array $formDatas): BoardEntity
{
//계층형 Reply
$entity = $this->reply_hierarchy(new BoardEntity(), $parent_entity);
return $this->create_process($entity, $formDatas);
}
//Index관련
public function setIndexWordFilter(string $word)
{
parent::setIndexWordFilter($word);
$this->orLike('title', $word, 'both');
$this->orLike('content', $word, 'both'); //befor , after , both
}
public function setIndexOrderBy($field, $order = 'DESC')
{
$this->orderBy("grpno", "DESC");
$this->orderBy("grporder", "ASC");
parent::setIndexOrderBy($field, $order);
}
}