ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 基础BaselModel ``` ~~~ <?php namespace app\api\model; use think\Model; use traits\model\SoftDelete; class BaseModel extends Model { // 软删除,设置后在查询时要特别注意whereOr // 使用whereOr会将设置了软删除的记录也查询出来 // 可以对比下SQL语句,看看whereOr的SQL use SoftDelete; protected $hidden = ['delete_time']; protected function prefixImgUrl($value, $data){ $finalUrl = $value; if($data['from'] == 1){ $finalUrl = config('setting.img_prefix').$value; } return $finalUrl; } } ~~~ ``` ## 其他的model继承 ~~~ <?php namespace app\api\model; use think\Model; class Category extends BaseModel { public function products() { return $this->hasMany('Product', 'category_id', 'id'); } public function img() { return $this->belongsTo('Image', 'topic_img_id', 'id'); } public static function getCategories($ids) { $categories = self::with('products') ->with('products.img') ->select($ids); return $categories; } public static function getCategory($id) { $category = self::with('products') ->with('products.img') ->find($id); return $category; } } ~~~ ## 控制器里面调用 ## ``` ~~~ public function getCategory($id) { $validate = new IDMustBePositiveInt(); $validate->goCheck(); $category = CategoryModel::getCategory($id); if(empty($category)){ throw new MissException([ 'msg' => 'category not found' ]); } return $category; } ~~~ ```