89 lines
3.2 KiB
PHP
89 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\BoardEntity;
|
|
|
|
class BoardModel extends CommonModel
|
|
{
|
|
protected $table = 'tw_board';
|
|
// protected $primaryKey = 'uid';
|
|
// protected $useAutoIncrement = true;
|
|
protected $allowedFields = ['grpno', 'grpord', 'grpdpt', 'board_category', 'user_uid', 'title', 'content', 'passwd', 'view_cnt', 'status', 'updated_at'];
|
|
protected $validationRules = [
|
|
'grpno' => 'if_exist|numeric',
|
|
'grpord' => 'if_exist|numeric',
|
|
'grpdpt' => 'if_exist|numeric',
|
|
'board_category' => 'required|string',
|
|
'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;
|
|
}
|
|
private function changeFormData($field, $value)
|
|
{
|
|
switch ($field) {
|
|
case 'passwd':
|
|
return password_hash($value, PASSWORD_DEFAULT);
|
|
break;
|
|
case 'content':
|
|
return htmlentities($value);
|
|
break;
|
|
}
|
|
return $value;
|
|
}
|
|
public function create(array $formDatas): BoardEntity
|
|
{
|
|
$entity = new BoardEntity($formDatas);
|
|
foreach ($formDatas as $field => $value) {
|
|
$entity->$field = $this->changeFormData($field, $value);
|
|
}
|
|
return parent::create_process($entity);
|
|
}
|
|
public function modify(BoardEntity $entity, array $formDatas): BoardEntity
|
|
{
|
|
foreach ($formDatas as $field => $value) {
|
|
$entity->$field = $this->changeFormData($field, $value);
|
|
}
|
|
return parent::modify_process($entity);
|
|
}
|
|
|
|
//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("grpord", "ASC");
|
|
parent::setIndexOrderBy($field, $order);
|
|
}
|
|
}
|