93 lines
3.2 KiB
PHP
93 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\OrderEntity;
|
|
|
|
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(array $fields = array())
|
|
{
|
|
parent::__construct('Order');
|
|
$this->allowedFields = ["uid", "user_uid", "name", ...$this->allowedFields, ...$fields];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
final public function getTitleField(): string
|
|
{
|
|
return 'name';
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case 'product_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 $this->getTitleField():
|
|
$rules[$field] = "required|trim|string";
|
|
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->getPrimaryKey(), 'product_uid']);
|
|
$this->_product_options = $productModel->getOptions();
|
|
}
|
|
$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");
|
|
}
|
|
|
|
//장바구니에 넣기
|
|
public function addCart(array $formDatas): OrderEntity
|
|
{
|
|
return $this->create_process(new OrderEntity(), $formDatas);
|
|
}
|
|
//장바구니에 빼기
|
|
public function canelCart(OrderEntity $entity)
|
|
{
|
|
return $this->delete(new $entity->getPrimaryKey());
|
|
}
|
|
}
|