You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
7.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Support\Facades\DB;
  4. class Article extends BaseModel
  5. {
  6. //文章模型
  7. /**
  8. * 关联到模型的数据表
  9. *
  10. * @var string
  11. */
  12. protected $table = 'article';
  13. /**
  14. * 表明模型是否应该被打上时间戳
  15. * 默认情况下,Eloquent 期望 created_at 和updated_at 已经存在于数据表中,如果你不想要这些 Laravel 自动管理的数据列,在模型类中设置 $timestamps 属性为 false
  16. *
  17. * @var bool
  18. */
  19. public $timestamps = false;
  20. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  21. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  22. /**
  23. * The connection name for the model.
  24. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  25. * @var string
  26. */
  27. //protected $connection = 'connection-name';
  28. const IS_CHECK = 0; // 已审核
  29. const UN_CHECK = 1; // 未审核
  30. //常用字段
  31. protected static $common_field = array(
  32. 'id', 'typeid', 'tuijian', 'click', 'title', 'writer', 'source','litpic', 'pubdate', 'addtime', 'description', 'listorder'
  33. );
  34. public static function getDb()
  35. {
  36. return DB::table('article');
  37. }
  38. /**
  39. * 获取关联到文章的分类
  40. */
  41. public function arctype()
  42. {
  43. return $this->belongsTo(Arctype::class, 'typeid', 'id');
  44. }
  45. /**
  46. * 列表
  47. * @param array $where 查询条件
  48. * @param string $order 排序
  49. * @param string $field 字段
  50. * @param int $offset 偏移量
  51. * @param int $limit 取多少条
  52. * @return array
  53. */
  54. public static function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 10)
  55. {
  56. $model = self::getDb();
  57. if($where){$res = $res->where($where);}
  58. $res['count'] = $model->count();
  59. $res['list'] = array();
  60. if($res['count'] > 0)
  61. {
  62. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  63. if($order){$model = $model->orderBy($order);}
  64. if($offset){}else{$offset = 0;}
  65. if($limit){}else{$limit = 10;}
  66. $res['list'] = $model->skip($offset)->take($limit)->get();
  67. }
  68. return $res;
  69. }
  70. /* public static function getList(array $param)
  71. {
  72. extract($param); //参数:group_id,limit,offset
  73. $limit = isset($limit) ? $limit : 10;
  74. $offset = isset($offset) ? $offset : 0;
  75. $model = new Article;
  76. if(isset($typeid)){$where['typeid'] = $typeid;}
  77. if(isset($ischeck)){$where['ischeck'] = $ischeck;}
  78. if(isset($keyword)){$model = $model->where("title", "like", "%$keyword%");} //关键词搜索
  79. if($where){$model = $model->where($where);}
  80. $res['count'] = $model->count();
  81. $res['list'] = array();
  82. //排序
  83. if(isset($orderby))
  84. {
  85. switch ($orderby)
  86. {
  87. case 1:
  88. $model = $model->orderBy('click','desc'); //点击量从高到低
  89. break;
  90. case 2:
  91. $model = $model->orderBy('listorder','desc'); //排序
  92. break;
  93. case 3:
  94. $model = $model->orderBy('pubdate','desc'); //更新时间从高到低
  95. break;
  96. default:
  97. $model = $model->orderBy('addtime','desc'); //添加时间从高到低
  98. }
  99. }
  100. if($res['count']>0)
  101. {
  102. $res['list'] = $model->select(self::$common_field)->orderBy('id', 'desc')->skip($offset)->take($limit)->get();
  103. }
  104. return $res;
  105. } */
  106. /**
  107. * 分页,用于前端html输出
  108. * @param array $where 查询条件
  109. * @param string $order 排序
  110. * @param string $field 字段
  111. * @param int $limit 每页几条
  112. * @param int $page 当前第几页
  113. * @return array
  114. */
  115. public static function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  116. {
  117. $res = self::getDb();
  118. if($where){$res = $res->where($where);}
  119. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  120. if($order){$res = $res->orderBy($order);}
  121. if($limit){}else{$limit = 10;}
  122. return $res->paginate($limit);
  123. }
  124. /**
  125. * 查询全部
  126. * @param array $where 查询条件
  127. * @param string $order 排序
  128. * @param string $field 字段
  129. * @param int $limit 取多少条
  130. * @return array
  131. */
  132. public static function getAll($where = array(), $order = '', $field = '*', $limit = 10, $offset = 0)
  133. {
  134. $res = self::getDb();
  135. if($where){$res = $res->where($where);}
  136. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  137. if($order){$res = $res->orderBy($order);}
  138. if($offset){}else{$offset = 0;}
  139. if($limit){}else{$limit = 10;}
  140. $res = $res->skip($offset)->take($limit)->get();
  141. return $res;
  142. }
  143. /**
  144. * 获取一条
  145. * @param array $where 条件
  146. * @param string $field 字段
  147. * @return array
  148. */
  149. public static function getOne($where, $field = '*')
  150. {
  151. $res = self::getDb();
  152. if($where){$res = $res->where($where);}
  153. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  154. $res = $res->find();
  155. return $res;
  156. }
  157. /**
  158. * 添加
  159. * @param array $data 数据
  160. * @return int
  161. */
  162. public static function add(array $data,$type = 0)
  163. {
  164. if($type==0)
  165. {
  166. // 新增单条数据并返回主键值
  167. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  168. }
  169. elseif($type==1)
  170. {
  171. /**
  172. * 添加单条数据
  173. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  174. * 添加多条数据
  175. * $data = [
  176. * ['foo' => 'bar', 'bar' => 'foo'],
  177. * ['foo' => 'bar1', 'bar' => 'foo1'],
  178. * ['foo' => 'bar2', 'bar' => 'foo2']
  179. * ];
  180. */
  181. return self::insert($data);
  182. }
  183. }
  184. /**
  185. * 修改
  186. * @param array $data 数据
  187. * @param array $where 条件
  188. * @return bool
  189. */
  190. public static function edit($data, $where = array())
  191. {
  192. if (self::where($where)->update(parent::filterTableColumn($data,$this->table)) !== false)
  193. {
  194. return true;
  195. }
  196. return false;
  197. }
  198. /* public static function modify($where, array $data)
  199. {
  200. if (self::where($where)->update($data)!==false)
  201. {
  202. return true;
  203. }
  204. return false;
  205. } */
  206. //删除一条记录
  207. /* public static function remove($id)
  208. {
  209. return self::whereIn('id', explode(',', $id))->delete();
  210. } */
  211. /**
  212. * 删除
  213. * @param array $where 条件
  214. * @return bool
  215. */
  216. public static function del($where)
  217. {
  218. return self::where($where)->delete();
  219. }
  220. //是否审核
  221. public static function getIscheckAttr($data)
  222. {
  223. $arr = array(0 => '已审核', 1 => '未审核');
  224. return $arr[$data['ischeck']];
  225. }
  226. //是否栏目名称
  227. public static function getTypenameAttr($data)
  228. {
  229. return DB::table('arctype')->where(array('id'=>$data['typeid']))->value('name');
  230. }
  231. }