vhost/app/Models/DeviceModel.php
2024-05-15 16:30:58 +09:00

83 lines
2.7 KiB
PHP

<?php
namespace App\Models;
use App\Entities\DeviceEntity;
class DeviceModel extends BaseModel
{
const STATUS_OUTOFSTOCK = "outofstock";
const STATUS_SOLDOUT = "soldout";
protected $table = "tw_device";
protected $useAutoIncrement = false;
protected $returnType = DeviceEntity::class;
protected $useSoftDeletes = true;
public function __construct()
{
parent::__construct('Device');
$this->allowedFields = [
...$this->allowedFields,
'type', 'name', "photo", "cost", "price", "sale",
"stock", "content", "status"
];
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
}
public function getTitleField(): string
{
return 'name';
}
public function getFieldRule(string $field, array $rules, string $action = ""): array
{
switch ($field) {
case $this->getTitleField():
$rules[$field] = "required|trim|string";
break;
case 'type':
$rules[$field] = "required|string";
break;
case 'photo':
$rules[$field] = !$action ? "if_exist|string" : "is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},300]|max_dims[{$field},2048,768]";
break;
case 'cost':
case 'price':
case 'stock':
$rules[$field] = "required|numeric";
break;
case 'sale':
$rules[$field] = "if_exist|numeric";
break;
case "content":
$rules[$field] = "required|string";
break;
default:
$rules = parent::getFieldRule($field, $rules, $action);
break;
}
return $rules;
}
public function getEntity($conditions): DeviceEntity
{
return $this->where($conditions)->first() ?: throw new \Exception(__FUNCTION__ . "에서 {$this->getClassName()}의 해당 데이터가 없습니다.");
}
public function create(array $formDatas): DeviceEntity
{
return $this->create_process(new DeviceEntity(), $formDatas);
}
public function modify(DeviceEntity $entity, array $formDatas): DeviceEntity
{
return $this->modify_process($entity, $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
}
}
}