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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                > ### 定義一對一關聯 > 描述:一個用戶有一份資料 > ## hasOne('關聯的模型', '外鍵名', '主鍵名', 'join類型') > 有一點需要注意的是,關聯方法的命名規范是駝峰法,而關聯屬性則一般是小寫+下劃線的方式,系統在獲取的時候會自動轉換對應,讀取user_profile關聯屬性則對應的關聯方法應該是userProfile。 ``` // 模型中這樣定義 class User extends Model { // 一個User有一個Profile public function profile() { // 定義關聯(默認使用user_id作為外鍵進行關聯) return $this->hasOne('Profile'); // 如果外鍵為uid,則需要如此設置 return $this->hasOne('Profile','uid'); // 指定關聯的字段 // 如果使用的是join方式的關聯,不支持指定field字段 return $this->hasOne('Profile')->field('id,name,email'); // 綁定指定的字段到父模型(支持讀取器) return $this->hasOne('Profile','uid')->bind('nickname,email'); // 還可以給綁定到父模型的子模型字段指定別名 return $this->hasOne('Profile','uid')->bind([ 'email', 'truename' => 'nickname', 'profile_id' => 'id', ]); // 這樣可以直接在控制器這樣訪問 $user = User::get(1,'profile'); // 輸出Profile關聯模型的email屬性 echo $user->email; echo $user->profile_id; } } ``` > ### 關聯查找 ``` // 獲取關聯的屬性 $user = User::get(1); echo $user->profile->email; // 獲取指定條件的關聯屬性 $user = User::hasWhere('profile',['email'=>'thinkphp@qq.com'])->find(); echo $user->name; ``` > ### 關聯新增 ``` $user = User::get(1); // 如果還沒有關聯數據 則進行新增 $user->profile()->save(['email' => 'thinkphp']); // 動態新增屬性 $user = User::find(1); $user->profile->phone = '1234567890'; $user->profile->save(); ``` > ### 關聯更新 ``` $$user = User::get(1); $user->profile->email = 'thinkphp'; $user->profile->save(); // 或者 $user->profile->save(['email' => 'thinkphp']); ``` > ## 定義反向關聯 > ### belongsTo('關聯模型名','外鍵名','關聯表主鍵名',['模型別名定義'],'join類型'); ``` // Profile屬于User class Profile extends Model { public function user() { // 默認關聯的外鍵為user_id return $this->belongsTo('User'); // 如果需要指定關聯的外鍵 return $this->belongsTo('User','uid'); } } // 根據子模型獲取父模型的數據 profile = Profile::get(1); // 輸出User關聯模型的屬性 echo $profile->user->account; ``` > ### 關聯自動寫入更新和刪除 ``` // 關聯插入 $blog = new Blog; $blog->name = 'thinkphp'; $content = new Content; $content->data = '實例內容'; $blog->content = $content; $blog->together('content')->save(); // 關聯更新(同時更新當前模型與其關聯模型) $blog = Blog::get(1); $blog->title = '更改標題'; $blog->content->data = '更新內容'; $blog->together('content')->save(); // 關聯刪除 $blog = Blog::get(1); $blog->together('content')->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>

                              哎呀哎呀视频在线观看