shoppingmallv2/app/Models/OrderModel.php
2023-08-01 15:16:22 +09:00

106 lines
3.5 KiB
PHP

<?php
namespace App\Models;
use App\Entities\OrderEntity;
use App\Entities\ProductEntity;
class OrderModel extends BaseModel
{
private $_product_options = null;
protected $table = "tw_order";
protected $useAutoIncrement = false;
protected $returnType = OrderEntity::class;
protected $useSoftDeletes = true;
public function __construct()
{
parent::__construct('Order');
$this->allowedFields = ["uid", "user_uid", ...$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->getTitleField(), "quantity", "price", "status"];
switch ($action) {
case "index":
case "excel":
case "view":
return [$this->getTitleField(), "user_uid", "quantity", "price", "status", "updated_at", "created_at"];
break;
default:
return $fields;
break;
}
}
final public function getFieldFilters(): array
{
return [$this->getTitleField(), "user_uid", "status"];
}
final public function getFieldBatchFilters(): array
{
return ["status"];
}
protected function getFieldRule(string $field, array $rules, string $action = ""): array
{
switch ($field) {
case $this->getTitleField():
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 'quantity':
case 'price':
$rules[$field] = "required|numeric";
break;
default:
$rules = parent::getFieldRule($field, $rules, $action);
break;
}
return $rules;
}
//Field별 Form Option용
public function getFieldFormOption(string $field): array
{
switch ($field) {
case 'product_uid':
if (is_null($this->_product_options)) {
$productModel = new productModel();
$this->_product_options = $productModel->getFormOptions();
}
$options = $this->_product_options;
break;
default:
return parent::getFieldFormOption($field);
break;
}
if (!is_array($options)) {
throw new \Exception(__FUNCTION__ . "에서 {$this->getClassName()}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true));
}
return $options;
}
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->getTitleField(), $word, "both");
}
}