<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                網站一般都有banner輪播圖,我們該怎么建表呢 一個banner_item就代表banner中的其中一個圖,也就是banner_item **banner** | 字段 | 說明 | | --- | --- | | id | | | name | | | description | | | del_time | | | update_time | | ![](https://img.kancloud.cn/35/3c/353c18fa45c51798a0e57ac6534e6347_322x51.png) **banner_item** | 字段 | 說明 | | --- | --- | | id | | | img_id | | | key_word | | | del_time | | | banner_id | | | update_time | | ![](https://img.kancloud.cn/a4/4a/a44a05c1d8a7badd5cfdc18e33e3f045_408x84.png) **image** | 字段 | 說明 | | --- | --- | | id | | | url| | | from| | | del_time | | | update_time | | ![](https://img.kancloud.cn/6b/60/6b60f0ba2dc4b9f247b63e2e88643eef_323x567.png) 我們在banner控制器查詢的時候使用with會將關聯數據一起查詢出來 直接查詢:$banner = BannerModel::find($id) ![](https://img.kancloud.cn/ee/84/ee842970ea2bcd3a35f7e48ccf2700f2_607x219.png) 關聯查詢: $banner = BannerModel::with('items')->find($id) ![](https://img.kancloud.cn/28/dd/28dd61a40af36dec9c09acce62e977f8_642x408.png) 嵌套關聯查詢: $banner = BannerModel::with('items','items.img')->find($id) ![](https://img.kancloud.cn/6f/71/6f71fc737da38345931917fcf94e430d_591x409.png) 所以我們進一步優化封裝代碼 banner控制器 ``` namespace app\api\controller\v1; use app\api\controller\BaseController; use app\api\validate\IDMustBePositiveInt; use app\api\model\Banner as BannerModel; use app\lib\exception\MissException; /** * Banner資源 */ class Banner extends BaseController { // protected $beforeActionList = [ // 'checkPrimaryScope' => ['only' => 'getBanner'] // ]; /** * 獲取Banner信息 * @url /banner/:id * @http get * @param int $id banner id * @return array of banner item , code 200 * @throws MissException */ public function getBanner($id) { $validate = new IDMustBePositiveInt(); $validate->goCheck(); $banner = BannerModel::getBannerById($id); if (!$banner ) { throw new MissException([ 'msg' => '請求banner不存在', 'errorCode' => 40000 ]); } return $banner; } } ``` Banner模型 ``` namespace app\api\model; use think\Model; class Banner extends BaseModel { //方法名可以隨意,但是with參數必須和此方法名一致,并且,此方法名也是返回數據的數組的鍵名 public function items() { return $this->hasMany('BannerItem', 'banner_id', 'id'); } // /** * @param $id int banner所在位置 * @return Banner */ public static function getBannerById($id) { $banner = self::with(['items','items.img']) ->find($id); // $banner = BannerModel::relation('items,items.img') // ->find($id); return $banner; } } ``` BannerItem模型 ``` namespace app\api\model; use think\Model; class BannerItem extends BaseModel { protected $hidden = ['id', 'img_id', 'banner_id', 'delete_time']; public function img() { return $this->belongsTo('Image', 'img_id', 'id'); } // } ``` **Image ** ``` namespace app\api\model; use think\Model; class Image extends BaseModel { protected $hidden = ['delete_time', 'id', 'from']; public function getUrlAttr($value, $data) { return $this->prefixImgUrl($value, $data); } } ``` **BaseModel** ``` 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; } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看