<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之旅 廣告
                hasOne 和 belongsTo 都是一對一關系,區別: 在主表(不包含外鍵)的模型中建立關聯關系,用 hasOne? 在從表(包含外鍵)模型中建立關聯關系,用 belongsTo # **一對多關聯** >[info] ## hasMany('**關聯模型**','關聯模型**外鍵**','當前模型**主鍵**'); 例子: **article:** | id | tittle | content | | --- | --- |--- | | 1 | 這是文章標題 | 這是文章內容 | **comment:** | id | art_id | content | | --- | --- |--- | | 1 | 1 | 文章評論1 | | 2 | 1 | 文章評論2 | | 3 | 1 | 文章評論3 | ~~~ <?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'], ]); ~~~ ### **關聯刪除** 在刪除文章的同時刪除下面的評論 ~~~ $article = Article::with('comments')->find(1); $article->together(['comments'])->delete(); ~~~ # **定義相對的關聯** >[info]## belongsTo('關聯模型','外鍵', '關聯主鍵'); ~~~ <?php name app\admin\model; use think\Model; class Comment extends Model { public function article() { return $this->belongsTo(Article::class); } } ~~~
                  <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>

                              哎呀哎呀视频在线观看