100 lines
3.2 KiB
PHP
100 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\OrderEntity;
|
|
|
|
class OrderModel extends BaseModel
|
|
{
|
|
protected $table = "tw_order";
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = OrderEntity::class;
|
|
protected $useSoftDeletes = false;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->allowedFields = ["uid", "user_uid", "sess_id", ...$this->allowedFields, ...$this->getFields(),];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
final public function getTitleField(): string
|
|
{
|
|
return 'product_uid';
|
|
}
|
|
final public function getFields(string $action = ""): array
|
|
{
|
|
$fields = [$this->getTitle(), "quantity", "price", "status"];
|
|
switch ($action) {
|
|
case "index":
|
|
case "excel":
|
|
case "view":
|
|
return [$this->getTitle(), "user_uid", "sess_id", "quantity", "price", "status", "updated_at", "created_at"];
|
|
break;
|
|
default:
|
|
return $fields;
|
|
break;
|
|
}
|
|
}
|
|
final public function getFieldFilters(): array
|
|
{
|
|
return [$this->getTitle(), "user_uid", "status"];
|
|
}
|
|
final public function getFieldBatchFilters(): array
|
|
{
|
|
return ["status"];
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case $this->getTitle():
|
|
case "user_uid":
|
|
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
|
break;
|
|
case "sess_id":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
case 'quantity':
|
|
case 'price':
|
|
$rules[$field] = "required|numeric";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
|
|
protected function changeFormData(string $action, string $field, array $formDatas, $entity)
|
|
{
|
|
switch ($field) {
|
|
case "sess_id": //장바구니에 입력의 경우에만 자동으로 추가
|
|
if ($action == 'create') {
|
|
$entity->$field = $this->_session->session_id;
|
|
}
|
|
break;
|
|
default:
|
|
$entity = parent::changeFormData($action, $field, $formDatas, $entity);
|
|
break;
|
|
}
|
|
return $entity;
|
|
}
|
|
|
|
public function getEntity($conditions): OrderEntity
|
|
{
|
|
return $this->where($conditions)->first() ?: throw new \Exception("해당 데이터가 없습니다.\n" . var_export($conditions, true));
|
|
}
|
|
public function create(array $formDatas): OrderEntity
|
|
{
|
|
return $this->create_process(new OrderEntity(), $formDatas);
|
|
}
|
|
public function modify(OrderEntity $entity, array $formDatas): OrderEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike($this->getTitle(), $word, "both");
|
|
}
|
|
}
|