多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 基础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; } ~~~ ```