BlogModel.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 getDesending()
  50. {
  51. return $this->orderBy('Id', 'desc');
  52. }
  53. public function getAll()
  54. {
  55. return $this->orderBy('Id', 'desc')->findAll();
  56. }
  57. public function insertBlog($title,$article,$image,$slug,$date)
  58. {
  59. $data = [
  60. 'title' => $title,
  61. 'article' => $article,
  62. 'image'=> $image,
  63. 'slug'=>$slug,
  64. 'date'=>$date
  65. ];
  66. $this->save($data);
  67. return True;
  68. }
  69. public function deleteOne($id)
  70. {
  71. $this->where('Id',$id )->delete();
  72. //need to get name image associated with blog and unlink it
  73. return True;
  74. }
  75. public function amendBlog($blogId,$title,$article,$slug)
  76. {
  77. $this->Id= $blogId;
  78. $this->title= $title;
  79. $this->article = $article;
  80. $this->slug=$slug;
  81. $data =[
  82. 'title'=>$this->title,
  83. 'article'=>$this->article,
  84. 'slug'=>$this->slug
  85. ];
  86. $logic= $this->update($this->Id,$data);
  87. return $logic;
  88. }
  89. public function isEmpty()
  90. {
  91. $db = \Config\Database::connect("default");
  92. $query = $db->query("SELECT * FROM blog order by Id DESC LIMIT 3");
  93. $count = $query->getRowArray();
  94. return $count;
  95. }
  96. }