<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之旅 廣告
                > ## 一對多關聯的定義(注意一對多的關聯方法需要用復數s) > ### hasMany('關聯模型名','外鍵名','主鍵名',['模型別名定義']); ``` class Article extends Model { public function comments() { // 一篇文章有多個評論 return $this->hasMany('Comment'); // 指定外鍵(最好指定外鍵) return $this->hasMany('Comment','art_id'); // 指定關聯的字段 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(); // 指定子模型返回的字段列表 // 查詢評論狀態正常的文章 name和title $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'], ]); ``` > ## 定義反向關聯(用法同一對一關聯) ``` 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>

                              哎呀哎呀视频在线观看