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