VendorProductModel.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Models;
  3. use CodeIgniter\Model;
  4. class VendorProductModel extends Model
  5. {
  6. protected $table = 'vendor_products';
  7. protected $primaryKey = 'id';
  8. protected $useAutoIncrement = true;
  9. protected $returnType = 'array';
  10. protected $useSoftDeletes = true;
  11. protected $allowedFields = [
  12. 'vendor_id', 'name', 'description', 'category',
  13. 'price', 'image_url', 'is_featured', 'status'
  14. ];
  15. protected $useTimestamps = true;
  16. protected $createdField = 'created_at';
  17. protected $updatedField = 'updated_at';
  18. protected $deletedField = 'deleted_at';
  19. protected $validationRules = [
  20. 'vendor_id' => 'required|integer|is_not_unique[vendors.id]',
  21. 'name' => 'required|max_length[255]',
  22. 'description' => 'permit_empty|max_length[65535]',
  23. 'category' => 'permit_empty|max_length[100]',
  24. 'price' => 'permit_empty|decimal|greater_than_equal_to[0]',
  25. 'image_url' => 'permit_empty|max_length[500]|valid_url',
  26. 'is_featured' => 'permit_empty|in_list[0,1]',
  27. 'status' => 'required|in_list[ACTIVE,INACTIVE,DISCONTINUED]'
  28. ];
  29. protected $validationMessages = [
  30. 'vendor_id' => [
  31. 'required' => '벤더사 id는 필수입니다.',
  32. 'is_not_unique' => '존재하지 않는 벤더사입니다.'
  33. ],
  34. 'name' => [
  35. 'required' => '제품명은 필수입니다.'
  36. ],
  37. 'price' => [
  38. 'greater_than_equal_to' => '가격은 0 이상이어야 합니다.'
  39. ],
  40. 'image_url' => [
  41. 'valid_url' => '유효하지 않은 이미지 URL입니다.'
  42. ]
  43. ];
  44. // 벤더사별 주요 제품 조회
  45. public function getFeaturedProducts($vendorId, $limit = 5)
  46. {
  47. return $this->where('vendor_id', $vendorId)
  48. ->where('status', 'ACTIVE')
  49. ->where('is_featured', 1)
  50. ->limit($limit)
  51. ->orderBy('created_at', 'DESC')
  52. ->findAll();
  53. }
  54. }