<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之旅 廣告
                ## 一對多關聯 ### 關聯定義 一對多關聯的情況也比較常見,使用`hasMany`方法定義, 參數包括: >[info] ### hasMany('關聯模型名','外鍵名','主鍵名',['模型別名定義']); 例如一篇文章可以有多個評論 ~~~ <?php namespace app\index\model; use think\Model; class Article extends Model { public function comments() { return $this->hasMany('Comment'); } } ~~~ 同樣,也可以定義外鍵的名稱 ~~~ <?php namespace app\index\model; use think\Model; class Article extends Model { public function comments() { return $this->hasMany('Comment','art_id'); } } ~~~ 如果需要指定查詢字段,可以使用下面的方式: ~~~ <?php namespace app\index\model; use think\Model; class Article extends Model { public function comments() { return $this->hasMany('Comment')->field('id,author,content'); } } ~~~ ### 關聯查詢 我們可以通過下面的方式獲取關聯數據 ~~~ $article = Article::get(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(); ~~~ >[danger] `V5.0.13+`版本開始,`hasWhere`方法新增`fields`參數,用于指定返回的字段列表。例如: ~~~ // 查詢評論狀態正常的文章 $list = Article::hasWhere('comments', ['status'=>1], 'name,title') ->select(); ~~~ ### 關聯新增 ~~~ $article = Article::find(1); // 增加一個關聯數據 $article->comments()->save(['content'=>'test']); // 批量增加關聯數據 $article->comments()->saveAll([ ['content'=>'thinkphp'], ['content'=>'onethink'], ]); ~~~ ### 定義相對的關聯 要在 Comment 模型定義相對應的關聯,可使用 belongsTo 方法: ~~~ <?php name app\index\model; use think\Model; class Comment extends Model { public function article() { return $this->belongsTo('article'); } } ~~~
                  <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>

                              哎呀哎呀视频在线观看