<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之旅 廣告
                獲取器: 獲取器 獲取數據對象的值;如果存在`'get'.$name.'Attr'`方法則調用它,否則查看type屬性里是否以鍵的形式存在,存在即轉換類型,否則查看是是createTime和updateTime屬性的值,是則轉換時間格式,那么就查找當前Model類對象的data屬性里是否有此鍵,是則返回,否則將$name當做關聯方法執行(該屬性為駝峰或小寫+下劃線格式都可以,最后都會轉化為駝峰格式執行),關聯方法也不存在的話 即上面都不存在則拋出錯誤 ``` namespace app\index\model; use think\Model; use think\Db; class Banner extends Model{ $type=['items'=>'integer']; $createTime="items"; public function items(){ return $this->hasMany('BannerItem','banner_id','id'); } public function getItemsAttr($value,$data){ //... } } ``` ### **例子:拼裝url** 如image表的url字段有個數據為/woman.jpg? 并且我們將圖片存到public/images目錄下的 application/setting.php 定義 ~~~ return [ 'img_prefix'=>'http://www.tp5.lichihua.com/images' ]; ~~~ 獲取可以 ~~~ $img_prefix=config('setting.img_prefix'); ~~~ 組裝:我么可以在model/Image.php中定義一個讀取器的方法格式為 get+當前模型的某個字段名+Attr ~~~ <?php namespace app\api\model; use think\Model; class Image extends Model{ protected $hidden=['id','from','delete_time','update_time']; /** * url讀取器 組裝之后用model查詢出來的url就是完整的路徑了 * from字段為1表示本站圖片 2為網絡資源即帶有http:完整url路徑 * @param max $value 自動獲取的image表中url字段的值 * @param max $data image的所有字段數據 * @return string 返回組裝好圖片的url地址 */ public function getUrlAttr($value,$data){ $finalUrl=$value; if ($data['from']==1) { $finalUrl = config('setting.img_prefix').$value; } return $finalUrl; } } ~~~ ?讀取器觸發的方式,模型調用字段屬性的時候自動調用 如下面的代碼也能調用getUrlAttr讀取器方法:上面的getBannerById方法在?return $banner;時由于關聯模型會關聯到Image框架會自動的調用Image模型的每一個模型屬性沒有顯示的調用! ~~~ $img=new Image(); $img->url; ~~~ 但是其他的模型字段也有類似的需求呢?我們就需要新建一個基類集成mode然后其他的model集成這個基類然后將讀取器放到基類里, 但是有個弊端當繼承基類的模型可能隱式的調用有奪取器的字段時,他會自動調用讀取器的,這不是我們所期望的,所以我們需要改造一下! ~~~ <?php namespace app\api\model; use think\Model; class BaseModel extends Model { public function prefixImgUrl($value,$data){ $finalUrl=$value; if ($data['from']==1) { $finalUrl = config('setting.img_prefix').$value; } return $finalUrl; } } <?php namespace app\api\model; use app\api\model\BaseModel; class Image extends BaseModel{ protected $hidden=['id','from','delete_time','update_time']; public function getUrlAttr($value,$data){ return $this->prefixImgUrl($value,$data); } } ~~~ 調用解析步驟 ``` new \app\index\model\banner()->getAttr('items'); //先找getItemsAttr() 這里會將$name的值轉為駝峰在組裝,沒找到再找$this->type['items']是否存在,沒找到再找$this->createTime的值,還沒找到繼續查找data屬性里是否有此鍵,有則返回值, 還沒找到在找items關聯方法,否則直接拋出錯誤 還有中在model中調用不存在的屬性時也會觸發,這是最常用的 所以在定義 獲取器方法、定義type、createTime、updateTime屬性和關聯方法時格外注意 比如在定義關聯方法時不要與表字段名一樣,定義了get字段名Attr方法時,就不要在type屬性里定義這個字段了,type定義的會被忽略 ```
                  <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>

                              哎呀哎呀视频在线观看