<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之旅 廣告
                > ## 多態一對多關聯 > 描述:有書籍表、文章表、評論表三張表,書籍可以有多個評論,文章也可以有多個評論 > ### morphMany('關聯模型名','多態字段信息','多態類型'); >1. 關聯模型名(必須):關聯的模型名稱,可以使用模型名(如Comment)或者完整的命名空間模型名(如app\index\model\Comment)。 >2. 多態字段信息(可選):支持兩種方式定義 如果是字符串表示多態字段的前綴(如commentable),多態字段使用 多態前綴_type和多態前綴_id,如果是數組,表示使用['多態類型字段名','多態ID字段名'],默認為當前的關聯方法名作為字段前綴。 >3. 多態類型(可選):當前模型對應的多態類型,默認為當前模型名,可以使用模型名(如Article)或者完整的命名空間模型名(如app\index\model\Article) 假設表的結構如下: > ### morphTo('多態字段信息',['多態類型別名']); > 1. 多態字段信息(可選):支持兩種方式定義 如果是字符串表示多態字段的前綴,多態字段使用 多態前綴_type和多態前綴_id,如果是數組,表示使用['多態類型字段名','多態ID字段名'],默認為當前的關聯方法名作為字段前綴 >2. 多態類型別名(可選):數組方式定義 ``` article id - integer-文章id title - string-文章標題 content - text- 文章內容 book id - integer-書籍id title - string-書籍名稱 comment id - integer-評論id content - text-評論內容 commentable_id - integer (文章|書籍)id commentable_type - string 評論的類型(文章|書籍) ``` ### 關聯定義 ``` // 文章多態關聯定義 class Article extends Model { /** * 獲取所有針對文章的評論。 */ public function comments() { return $this->morphMany('Comment', 'commentable'); } } // 書籍多態關聯定義 class Book extends Model { /** * 獲取所有針對書籍的評論。 */ public function comments() { return $this->morphMany('Comment', 'commentable'); } } // 評論多態關聯定義 class Comment extends Model { /** * 獲取評論對應的多態模型。 */ public function commentable() { // 默認以模型名作為多態表類型(commentable_type)的劃分,如Article,Book return $this->morphTo(); // 可以手動設置多態表類型名 return $this->morphTo('commentable',[ 'book' => 'app\index\model\Book', 'post' => 'app\admin\model\Article', ]); } } ``` ### 關聯查找 ``` // 查找文章對應的評論 $article = Article::get(1); foreach ($article->comments as $comment) { dump($comment); } // 查找評論所屬的類型(文章|書籍) $comment = Comment::get(1); $commentable = $comment->commentable; ``` > ## 多態一對一關聯 > 描述:有成員表、團隊表和頭像表三張表,成員可以有一個頭像,團隊也可以有一個頭像 > ### morphOne('關聯模型名','多態字段信息','多態類型'); > 1. 關聯模型名(必須):關聯的模型名稱,可以使用模型名(如Avatar)或者完整的命名空間模型名(如app\index\model\Avatar)。 >2. 多態字段信息(可選):支持兩種方式定義 如果是字符串表示多態字段的前綴(如imageable),多態字段使用 多態前綴_type和多態前綴_id,如果是數組,表示使用['多態類型字段名','多態ID字段名'],默認為當前的關聯方法名作為字段前綴。 >3. 多態類型(可選):當前模型對應的多態類型,默認為當前模型名,可以使用模型名(如Member)或者完整的命名空間模型名(如app\index\model\Member)。 ``` member id - integer - 成員id name - string - 成員姓名 team id - integer - 團隊id name - string - 團隊名稱 avatar id - integer - 頭像id avatar - string - 頭像路徑 imageable_id - integer - (成員|團隊)id imageable_type - string - 頭像類型(成員|團隊) ``` ### 關聯定義 ``` // 成員多態關聯定義 class Member extends Model { /** * 獲取用戶的頭像 */ public function avatar() { return $this->morphOne('Avatar', 'imageable'); } } // 團隊多態關聯定義 class Team extends Model { /** * 獲取團隊的頭像 */ public function avatar() { return $this->morphOne('Avatar', 'imageable'); } } // 頭像多態關聯定義 class Avatar extends Model { /** * 獲取頭像對應的多態模型。 */ public function imageable() { return $this->morphTo(); } } ```
                  <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>

                              哎呀哎呀视频在线观看