ProductModel.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php namespace App\Models;
  2. use CodeIgniter\Database\ConnectionInterface;
  3. use CodeIgniter\Model;
  4. class ProductModel extends Model
  5. {
  6. protected $table = 'products';
  7. protected $primaryKey = 'Id';
  8. protected $allowedFields = ['image','imageTitle','slug','category','info','price'];
  9. protected $limit;
  10. protected $offset;
  11. protected $Id;
  12. protected $imageTitle;
  13. protected $slug;
  14. protected $category;
  15. protected $info;
  16. /*
  17. $db = \Config\Database::connect();
  18. $builder = $db->table('products');
  19. */
  20. public function insertProduct($image,$imageTitle,$slug,$category,$info,$price)
  21. {
  22. $data = [
  23. 'image' => $image,
  24. 'imageTitle' => $imageTitle,
  25. 'slug'=> $slug,
  26. 'category'=>$category,
  27. 'info'=>$info,
  28. 'price'=>$price
  29. ];
  30. $this->save($data);
  31. }
  32. public function getAll()
  33. {
  34. return $this->orderBy('Id', 'desc')->findAll();
  35. }
  36. public function getOne($id)
  37. {
  38. return $this->find($id);
  39. // return $this->asArray()
  40. // ->where(['Id'=>$id]);
  41. }
  42. public function amendProduct($Id,$imageTitle,$slug,$category,$info,$price)
  43. {
  44. $data =[
  45. 'imageTitle'=>$imageTitle,
  46. 'slug'=>$slug,
  47. 'category'=>$category,
  48. 'info'=>$info,
  49. 'price'=>$price
  50. ];
  51. $logic= $this->update($Id,$data);
  52. return $logic;
  53. }
  54. public function deleteOne($id)
  55. {
  56. $this->where('Id',$id )->delete();
  57. //need to get name image associated with blog and unlink it
  58. return True;
  59. }
  60. public function getCountByCategory($category)
  61. {
  62. $db = \Config\Database::connect();
  63. $builder = $db->table('products');
  64. $query = $builder->where('category',$category)->countAllResults();
  65. return $query;
  66. /*
  67. $query = $db->query("SELECT COUNT(*) FROM products where category=$category ");
  68. $count = $query->getRowArray();
  69. return $count;
  70. */
  71. }
  72. public function useCategory($category)
  73. {
  74. return $this->where('category', $category)
  75. ->findAll();
  76. }
  77. }