BlogModel.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php namespace App\Models;
  2. use CodeIgniter\Database\ConnectionInterface;
  3. use CodeIgniter\Model;
  4. class BlogModel extends Model
  5. {
  6. protected $table = 'blog';
  7. protected $primaryKey = 'Id';
  8. protected $allowedFields = ['title','article','image','slug','date'];
  9. protected $limit;
  10. protected $offset;
  11. protected $Id;
  12. protected $title;
  13. protected $article;
  14. protected $slug;
  15. public function getArticle($slug)
  16. {
  17. return $this->asArray()
  18. ->where(['slug' => $slug])
  19. ->first();
  20. }
  21. public function count()
  22. {
  23. return $this->countAll();
  24. }
  25. public function getOneToDel($id)
  26. {
  27. return $this->find($id);
  28. // return $this->asArray()
  29. // ->where(['Id'=>$id]);
  30. }
  31. public function getOne()
  32. {
  33. $this->limit=1;
  34. $this->offset=0;
  35. return $this->orderBy('Id', 'desc')->findAll($this->limit, $this->offset);
  36. }
  37. public function getThree()
  38. {
  39. $this->limit=3;
  40. $this->offset=0;
  41. return $this->orderBy('Id', 'desc')->findAll($this->limit, $this->offset);
  42. }
  43. public function getSome()
  44. {
  45. $this->limit=2;
  46. $this->offset=1;
  47. return $this->orderBy('Id', 'desc')->findAll($this->limit, $this->offset);
  48. }
  49. public function getAll()
  50. {
  51. return $this->orderBy('Id', 'desc')->findAll();
  52. }
  53. public function insertBlog($title,$article,$image,$slug,$date)
  54. {
  55. $data = [
  56. 'title' => $title,
  57. 'article' => $article,
  58. 'image'=> $image,
  59. 'slug'=>$slug,
  60. 'date'=>$date
  61. ];
  62. $this->save($data);
  63. return True;
  64. }
  65. public function deleteOne($id)
  66. {
  67. $this->where('Id',$id )->delete();
  68. //need to get name image associated with blog and unlink it
  69. return True;
  70. }
  71. public function amendBlog($blogId,$title,$article,$slug)
  72. {
  73. $this->Id= $blogId;
  74. $this->title= $title;
  75. $this->article = $article;
  76. $this->slug=$slug;
  77. $data =[
  78. 'title'=>$this->title,
  79. 'article'=>$this->article,
  80. 'slug'=>$this->slug
  81. ];
  82. $logic= $this->update($this->Id,$data);
  83. return $logic;
  84. }
  85. public function isEmpty()
  86. {
  87. $db = \Config\Database::connect("default");
  88. $query = $db->query("SELECT * FROM blog order by Id DESC LIMIT 3");
  89. $count = $query->getRowArray();
  90. return $count;
  91. }
  92. }