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

                # 關聯模型 在實際開發中,表與表之間產生聯系,是特別常見的。這個時候查詢起來,就比較麻煩了,要么自己手動join寫查詢語句,要么使用join()連貫操作。 這里我們更建議使用模型中的“關聯”查詢功能,將復雜的查詢交給TP去做,我們自己只把控好我們自己的業務邏輯(系統中,就幾乎沒有自己手動join或使用join()連貫操作,全部使用關聯交給TP自己去查)。 TP本身的關聯操作,這里不做詳細介紹 → [關聯操作傳送門](http://www.hmoore.net/manual/thinkphp6_0/1037599)。 很多人不用關聯的原因是對關聯中幾種關系hasOne、belongsTo、hasMany理解不夠,這里我以前寫過一篇帖子,有興趣的可以在“問答”中看下。 ### 總覽格式 比如“文章”模型中,關聯有“欄目”、“用戶” ~~~ public $relationLink = array( 'Menu' => array( 'type' => 'belongsTo', 'foreignKey' => 'menu_id', 'localKey' => 'id', 'counterCache' => true ), 'User' => array( 'type' => 'belongsTo' ) ); ~~~ 可以同時定義多個關聯模型,格式:`關聯模型名 => 關聯屬性`。 這樣定義以后,就不用像TP那樣去定義一個關聯function了;然后使用的時候和TP一樣: ~~~ $article = model('Article')->find();//查詢文章 $article = $article->Menu;//關聯到Menu數據 $article = $article->User;//關聯到User數據 pr($article->toArray());//從對象中獲取到數據 //最后,得到的數據結構: Array ( [id] => 1 [menu_id] => 3 [user_id] => 1 [title] => 測試 ...... [Menu] => Array ( [id] => 3 [title] => 新聞 ...... ) [User] => Array ( [id] => 1 [username] => eduask ...... ) ) ~~~ * * * * * ### hasOne定義 ~~~ public $relationLink = [ '關聯模型名'=>[ 'type' => 'hasOne', 'foreign' => '關聯模型名', 'foreignKey' => '外鍵名', 'localKey' => '主鍵名', 'deleteWith' => true, 'field' => [關聯查詢時字段], 'where' => [關聯查詢時額外條件], 'order' => [關聯查詢時排序方式] ] ]; public $relationLink = [ '關聯模型名' => 'hasOne' ]; ~~~ `type`:string 關聯類型,**必須**含有該屬性且值為`hasOne` `foreign`:string 關聯模型名,沒有設置自動使用 數組所在鍵名 `foreignKey`:string 關聯外鍵名,默認為 `當前模型小寫形式_id`。 比如:User 對應 user_id ,AdPosition 對應 ad_position_id `localKey`:string 默認為"",交給tp自己處理(tp默認肯定就是`id`) `deleteWith`:boolean 關聯刪除,系統自己增加功能;默認不會關聯刪除 比如,`User`模型中: ~~~ public $relationLink = array( 'Profile' => array( 'type' => 'hasOne', 'deleteWith' => true ) ); ~~~ 當刪除一條用戶記錄時,關聯的用戶信息記錄也會被自動刪除 `field`:array 關聯查詢時字段,同`field()`用法,一般不會定義;后面幾個屬性同理。 * * * * * ### hasMany定義 ~~~ public $relationLink =[ '關聯模型名' => [ 'type' => 'hasMany', 'foreign' => '關聯模型名', 'foreignKey' => '外鍵名', 'localKey' => '主鍵名', 'deleteWith' => true, 'field' => [關聯查詢時字段], 'where' => [關聯查詢時額外條件], 'order' => [關聯查詢時排序方式], 'limit' => 2 ] ]; public $relationLink = [ '關聯模型名' => 'hasMany' ]; ~~~ `type`:string 關聯類型,**必須**含有該屬性且值為`hasMany` `foreign`:string 關聯模型名,沒有設置自動使用 數組所在鍵名 `foreignKey`:string 關聯外鍵名,默認為 `當前模型小寫形式_id`。 比如:User 對應 user_id ,AdPosition 對應 ad_position_id `localKey`:string 默認為"",交給tp自己處理(tp默認肯定就是`id`) `deleteWith`:boolean 關聯刪除,系統自己增加功能; 比如,`AdPosition`模型中: ~~~ public $relationLink = array( 'Ad' => array( 'type' => 'hasMany', 'deleteWith' => true ) ); ~~~ 當刪除一條廣告位記錄,關聯的所有廣告記錄都將會被自動刪除 `field`:array 關聯查詢時字段,同`filed()`用法,一般不會定義;后面幾個屬性同理。 * * * * * ### belongsTo 定義 ~~~ public $relationLink =[ '關聯模型名' => [ 'type' => 'belongsTo', 'foreign' => '關聯模型名', 'foreignKey' => '外鍵名', 'localKey' => '主鍵名', 'counterCache' => '關聯統計字段', 'counterWhere' => [關聯統計額外條件],// V2.3.1新增 'sumCache' => '關聯求和統計字段',// V2.3.1新增 'sumCacheTo' => '求和以后更新到父模型的字段名'// V2.3.1新增 'sumWhere' => [關聯求和統計額外條件],// V2.3.1新增 'field' => [關聯查詢時字段], 'where' => [關聯查詢時額外條件], 'order' => [關聯查詢時排序方式], ] ]; public $relationLink = [ '關聯模型名' => 'belongsTo' ]; ~~~ `type`:string 關聯類型,**必須**含有該屬性且值為`belongsTo` `foreign`:string 關聯模型名,沒有設置自動使用 數組所在鍵名 `foreignKey`:string 外鍵名,默認為 `關聯模型小寫形式_id`。 比如:User 對應 user_id ,AdPosition 對應 ad_position_id `localKey`:string 關聯表主鍵名 默認為"",交給tp自己處理(tp默認肯定就是`id`) `counterCache` :boolean 或 string 關聯統計計數,系統自己增加功能,默認不會統計;如果為true,關聯統計字段為 `當前模型小寫形式_count`,如果為字符串,表示以此為關聯統計字段。 比如,`Ad`模型中: ~~~ public $relationLink = array( 'AdPosition' => array( 'type' => 'belongsTo', 'counterCache' => true ) ); ~~~ 這樣你的AdPosition模型中需要有一個字段叫`ad_count`,每當廣告添加或刪除時,對應廣告位就會重新統計含有多少條廣告記錄并更新到該字段。 `field`:array 關聯查詢時字段,同`filed()`用法,一般不會定義;后面幾個屬性同理。 `sumCache`:string 關聯求和統計,系統自己增加功能,默認不會統計;還必須有 'sumCacheTo' 屬性:當前模型數據變動時會統計當前模型中sumCache所設字段之和然后更新到父模型(當前關聯模型)的sumCacheTo所設字段中。 `V2.3.4`以后支持多個字段,多個字段之間用逗號`,`分隔;相應的`sumCacheTo`也應該是對應的多個字段。 `sumCacheTo`:string 父模型的統計求和字段,需和sumCache屬性配合。 * * * * * ### belongsToMany 定義 ~~~ public $relationLink =[ '關聯模型名' => [ 'type' => 'belongsToMany', 'foreign' => '關聯模型名', 'middle' => '中間表名', 'foreignKey' => '外鍵名', 'localKey' => '當前模型關聯鍵名' ] ]; ~~~ `type`:string 關聯類型,**必須**含有該屬性且值為`belongsToMany` `middle`:string 中間表名,**必須**定義中間表對應的模型名 `foreign`:string 關聯模型名,沒有設置自動使用 數組所在鍵名 `foreignKey` :string,中間表的當前模型外鍵,默認的外鍵名規則是關聯模型名+`_id` `localKey`:string,中間表的當前模型關聯鍵名,默認規則是當前模型名+`_id` `deleteWith`:boolean,默認false;當前模型數據刪除以后,自動刪除中間表(非關聯模型數據)相關聯的數據
                  <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>

                              哎呀哎呀视频在线观看