<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 功能強大 支持多語言、二開方便! 廣告
                ## 聚合模型 >[danger] `5.0.5+`版本一對一關聯已經改進,支持關聯屬性綁定到主模型,以及支持自動關聯寫入,聚合模型的優勢已經不復存在,后面版本不會再更新聚合模型了。 |版本|調整功能| |---|---| |5.0.3|支持使用`field`屬性定義需要的字段| |5.0.2|`relationModel` 屬性改為非靜態定義| 通過聚合模型可以把**一對一關聯**的操作更加簡化,只需要把你的模型類繼承`think\model\Merge`,就可以自動完成關聯查詢、關聯保存和關聯刪除。 例如下面的用戶表關聯了檔案表,兩個表信息如下: ### think_user |字段名 |描述| |---|---| |id|主鍵| |name|用戶名| |password|密碼| |nickname|昵稱| ### think_profile |字段名 |描述| |---|---| |id|主鍵| |truename|真實姓名| |phone|電話| |email|郵箱| |user_id|用戶ID| 我們只需要定義好主表的模型,例如下面是User模型的定義: ~~~ <?php namespace app\index\model; use think\model\Merge; class User extends Merge { // 定義關聯模型列表 protected static $relationModel = ['Profile']; // 定義關聯外鍵 protected $fk = 'user_id'; protected $mapFields = [ // 為混淆字段定義映射 'id' => 'User.id', 'profile_id' => 'Profile.id', ]; } ~~~ >[danger] `V5.0.2+`版本`relationModel` 屬性不再使用`static`定義了。 如果需要單獨設置關聯數據表,可以使用: ~~~ namespace app\index\model; use think\model\Merge; class User extends Merge { // 設置主表名 protected $table = 'think_user'; // 定義關聯模型列表 protected static $relationModel = [ // 給關聯模型設置數據表 'Profile' => 'think_user_profile', ]; // 定義關聯外鍵 protected $fk = 'user_id'; protected $mapFields = [ // 為混淆字段定義映射 'id' => 'User.id', 'profile_id' => 'Profile.id', ]; } ~~~ >[danger] 注意:對于關聯表中存在混淆的字段名一定要通過mapFields屬性定義。 接下來,我們可以和使用普通模型一樣的方法來操作用戶模型及其關聯數據。 ~~~ // 關聯查詢 $user = User::get(1); echo $user->id; echo $user->name; echo $user->phone; echo $user->email; echo $user->profile_id; $user->email = 'thinkphp@qq.com'; // 關聯保存 $user->save(); // 關聯刪除 $user->delete(); // 根據主鍵關聯刪除 User::destroy([1,2,3]); ~~~ 操作兩個數據表就和操作一個表一樣的感覺,關聯表的寫入、更新和刪除自動采用事務(只要數據庫支持事務),一旦主表寫入失敗或者發生異常就會發生回滾。 如果主表除了Profile關聯之外,還有其他的一對多關聯,一樣可以定義額外的關聯,例如: ~~~ namespace app\index\model; use think\model\Merge; class User extends Merge { // 定義關聯模型列表 protected static $relationModel = ['Profile']; // 定義關聯外鍵 protected $fk = 'user_id'; protected $mapFields = [ // 為混淆字段定義映射 'id' => 'User.id', 'profile_id' => 'Profile.id', ]; public function articles(){ return $this->hasMany('Article'); } } ~~~ 對一對多關聯進行操作,例如: ~~~ $user = User::get(1); // 讀取關聯信息 dump($user->articles); // 或者進行關聯預載入 $user = User::get(1,'articles'); ~~~ >[danger] 注意:不能再次對 已經在`relationModel`屬性中定義過的關聯表進行關聯定義和預載入查詢。
                  <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>

                              哎呀哎呀视频在线观看