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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 一對多關聯 ### 關聯定義 一對多關聯的情況也比較常見,使用`hasMany`方法定義,參數包括: >[info] ### hasMany('關聯模型','外鍵','主鍵'); 除了關聯模型外,其它參數都是可選。 * **關聯模型**(必須):關聯模型類名 * **外鍵**:關聯模型外鍵,默認的外鍵名規則是當前模型名+`_id` * **主鍵**:當前模型主鍵,一般會自動獲取也可以指定傳入 例如一篇文章可以有多個評論 ~~~ <?php namespace app\model; use think\Model; class Article extends Model { public function comments() { return $this->hasMany(Comment::class); } } ~~~ 同樣,也可以定義外鍵的名稱 ~~~ <?php namespace app\model; use think\Model; class Article extends Model { public function comments() { return $this->hasMany(Comment::class,'art_id'); } } ~~~ ### 關聯查詢 我們可以通過下面的方式獲取關聯數據 ~~~ $article = Article::find(1); // 獲取文章的所有評論 dump($article->comments); // 也可以進行條件搜索 dump($article->comments()->where('status',1)->select()); ~~~ ### 根據關聯條件查詢 可以根據關聯條件來查詢當前模型對象數據,例如: ~~~ // 查詢評論超過3個的文章 $list = Article::has('comments','>',3)->select(); // 查詢評論狀態正常的文章 $list = Article::hasWhere('comments',['status'=>1])->select(); ~~~ 如果需要更復雜的關聯條件查詢,可以使用 ``` $where = Comment::where('status',1)->where('content', 'like', '%think%'); $list = Article::hasWhere('comments', $where)->select(); ``` ### 關聯新增 ~~~ $article = Article::find(1); // 增加一個關聯數據 $article->comments()->save(['content'=>'test']); // 批量增加關聯數據 $article->comments()->saveAll([ ['content'=>'thinkphp'], ['content'=>'onethink'], ]); ~~~ ### 定義相對的關聯 要在 Comment 模型定義相對應的關聯,可使用 `belongsTo` 方法: ~~~ <?php name app\model; use think\Model; class Comment extends Model { public function article() { return $this->belongsTo(Article::class); } } ~~~ ### 關聯刪除 在刪除文章的同時刪除下面的評論 ~~~ $article = Article::with('comments')->find(1); $article->together(['comments'])->delete(); ~~~
                  <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>

                              哎呀哎呀视频在线观看