VendorAddressModel.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Models;
  3. use CodeIgniter\Model;
  4. class VendorAddressModel extends Model
  5. {
  6. protected $table = 'vendor_addresses';
  7. protected $primaryKey = 'id';
  8. protected $useAutoIncrement = true;
  9. protected $returnType = 'array';
  10. protected $useSoftDeletes = false;
  11. protected $allowedFields = [
  12. 'vendor_id', 'address_type', 'zip_code', 'address',
  13. 'detail_address', 'city', 'district', 'is_primary'
  14. ];
  15. protected $useTimestamps = true;
  16. protected $createdField = 'created_at';
  17. protected $updatedField = 'updated_at';
  18. protected $validationRules = [
  19. 'vendor_id' => 'required|integer|is_not_unique[vendors.id]',
  20. 'address_type' => 'required|in_list[HEAD_OFFICE,BRANCH,WAREHOUSE,BILLING]',
  21. 'zip_code' => 'permit_empty|max_length[10]',
  22. 'address' => 'required|max_length[500]',
  23. 'detail_address' => 'permit_empty|max_length[500]',
  24. 'city' => 'permit_empty|max_length[100]',
  25. 'district' => 'permit_empty|max_length[100]',
  26. 'is_primary' => 'permit_empty|in_list[0,1]'
  27. ];
  28. protected $validationMessages = [
  29. 'vendor_id' => [
  30. 'required' => '벤더사 id는 필수입니다.',
  31. 'is_not_unique' => '존재하지 않는 벤더사입니다.'
  32. ],
  33. 'address' => [
  34. 'required' => '주소는 필수입니다.'
  35. ]
  36. ];
  37. // 기본 주소로 설정
  38. public function setPrimaryAddress($vendorId, $addressId)
  39. {
  40. $this->db->transStart();
  41. // 기존 기본 주소 해제
  42. $this->where('vendor_id', $vendorId)
  43. ->set('is_primary', 0)
  44. ->update();
  45. // 새로운 기본 주소 설정
  46. $this->update($addressId, ['is_primary' => 1]);
  47. $this->db->transComplete();
  48. return $this->db->transStatus();
  49. }
  50. }