<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                多態一對一相比多態一對多關聯的區別是動態的一對一關聯,舉個例子說有一個個人和團隊表,而無論個人還是團隊都有一個頭像需要保存但都會對應同一個頭像表 ~~~ member id - integer name - string team id - integer name - string avatar id - integer avatar - string imageable_id - integer imageable_type - string ~~~ ## 關聯定義 會員模型: ~~~ <?php namespace app\model; use think\Model; class Member extends Model { /** * 獲取用戶的頭像 */ public function avatar() { return $this->morphOne(Avatar::class, 'imageable'); } } ~~~ 團隊模型: ~~~ <?php namespace app\model; use think\Model; class Team extends Model { /** * 獲取團隊的頭像 */ public function avatar() { return $this->morphOne(Avatar::class, 'imageable'); } } ~~~ `morphOne`方法的參數如下: >[info] ### morphOne('關聯模型','多態字段','多態類型'); **關聯模型**(必須):關聯的模型類名。 **多態字段**(可選):支持兩種方式定義 如果是字符串表示多態字段的前綴,多態字段使用 `多態前綴_type`和`多態前綴_id`,如果是數組,表示使用['多態類型字段名','多態ID字段名'],默認為當前的關聯方法名作為字段前綴。 **多態類型**(可選):當前模型對應的多態類型,默認為當前模型名,可以使用模型名(如`Member`)或者完整的命名空間模型名(如`app\index\model\Member`)。 下面是頭像模型的關聯定義: ~~~ <?php namespace app\model; use think\Model; class Avatar extends Model { /** * 獲取頭像對應的多態模型。 */ public function imageable() { return $this->morphTo(); } } ~~~ 理解了多態一對多關聯后,多態一對一關聯其實就很容易理解了,區別就是當前模型和動態關聯的模型之間的關聯屬于一對一關系。 ## 綁定屬性到父模型(`V2.0.40+`) 可以在定義關聯的時候使用`bind`方法綁定屬性到父模型,例如: ~~~ <?php namespace app\model; use think\Model; class Team extends Model { /** * 獲取團隊的頭像 */ public function avatar() { return $this->morphOne(Avatar::class, 'imageable')->bind(['nickname', 'email']); } } ~~~ 或者指定綁定屬性別名 ~~~ <?php namespace app\model; use think\Model; class Team extends Model { /** * 獲取團隊的頭像 */ public function avatar() { return $this->morphOne(Avatar::class, 'imageable')->bind([ 'email', 'truename' => 'nickname', ]); } } ~~~ 然后使用關聯預載入查詢的時候,可以使用 ~~~ $team = Team::with('avatar')->find(1); // 直接輸出Avatar關聯模型的綁定屬性 echo $team->email; echo $team->truename; ~~~ 綁定關聯模型的屬性支持讀取器。
                  <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>

                              哎呀哎呀视频在线观看