// +---------------------------------------------------------------------- namespace app\model; use think\Model; use think\model\concern\SoftDelete; /** * 文章模型 */ class Articles extends Model { // 启用软删除 use SoftDelete; // 数据库表名 protected $name = 'mete_articles'; // 自动写入时间戳 protected $autoWriteTimestamp = true; // 时间戳字段名 protected $createTime = 'create_time'; protected $updateTime = 'update_time'; protected $deleteTime = 'delete_time'; // 字段类型转换 protected $type = [ 'id' => 'integer', 'cate' => 'integer', 'sort' => 'integer', 'status' => 'integer', 'is_trans' => 'integer', 'views' => 'integer', 'likes' => 'integer', 'recommend' => 'integer', 'top' => 'integer', 'publisher' => 'integer', 'create_time' => 'datetime', 'update_time' => 'datetime', 'publishdate' => 'datetime', 'delete_time' => 'datetime', ]; /** * 关联文章分类 */ public function category() { return $this->belongsTo('app\index\model\ArticleCategory', 'cate', 'id', [], 'LEFT')->bind([ 'cate_name' => 'name', ]); } /** * 关联发布者 */ public function publisher() { return $this->belongsTo('app\index\model\AdminUser', 'publisher', 'id', [], 'LEFT')->bind([ 'publisher_name' => 'name', ]); } /** * 获取已发布的文章 */ public static function published() { return self::where('status', 2)->where('delete_time', null); } /** * 获取推荐文章 */ public static function recommended() { return self::published()->where('recommend', 1); } /** * 获取置顶文章 */ public static function top() { return self::published()->where('top', 1); } /** * 按分类获取文章 */ public static function byCategory($cateId) { return self::published()->where('cate', $cateId); } /** * 按关键词搜索文章 */ public static function search($keyword) { return self::published()->where('title', 'like', '%' . $keyword . '%'); } /** * 更新阅读量 */ public function incrementViews() { return $this->where('id', $this->id)->inc('views')->update(); } /** * 更新点赞量 */ public function incrementLikes() { return $this->where('id', $this->id)->inc('likes')->update(); } }