久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<tfoot id='3Qxi3'></tfoot>

    <small id='3Qxi3'></small><noframes id='3Qxi3'>

      • <bdo id='3Qxi3'></bdo><ul id='3Qxi3'></ul>

        <i id='3Qxi3'><tr id='3Qxi3'><dt id='3Qxi3'><q id='3Qxi3'><span id='3Qxi3'><b id='3Qxi3'><form id='3Qxi3'><ins id='3Qxi3'></ins><ul id='3Qxi3'></ul><sub id='3Qxi3'></sub></form><legend id='3Qxi3'></legend><bdo id='3Qxi3'><pre id='3Qxi3'><center id='3Qxi3'></center></pre></bdo></b><th id='3Qxi3'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='3Qxi3'><tfoot id='3Qxi3'></tfoot><dl id='3Qxi3'><fieldset id='3Qxi3'></fieldset></dl></div>

      1. <legend id='3Qxi3'><style id='3Qxi3'><dir id='3Qxi3'><q id='3Qxi3'></q></dir></style></legend>

        當(dāng)同一模型也存在 HasMany 關(guān)系時(shí),如何更新 Has

        How to update a HasOne relationship when a HasMany relationship also exists with the same model?(當(dāng)同一模型也存在 HasMany 關(guān)系時(shí),如何更新 HasOne 關(guān)系?)

              <legend id='OcxcK'><style id='OcxcK'><dir id='OcxcK'><q id='OcxcK'></q></dir></style></legend>

              <small id='OcxcK'></small><noframes id='OcxcK'>

                <bdo id='OcxcK'></bdo><ul id='OcxcK'></ul>
              • <i id='OcxcK'><tr id='OcxcK'><dt id='OcxcK'><q id='OcxcK'><span id='OcxcK'><b id='OcxcK'><form id='OcxcK'><ins id='OcxcK'></ins><ul id='OcxcK'></ul><sub id='OcxcK'></sub></form><legend id='OcxcK'></legend><bdo id='OcxcK'><pre id='OcxcK'><center id='OcxcK'></center></pre></bdo></b><th id='OcxcK'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='OcxcK'><tfoot id='OcxcK'></tfoot><dl id='OcxcK'><fieldset id='OcxcK'></fieldset></dl></div>
              • <tfoot id='OcxcK'></tfoot>

                    <tbody id='OcxcK'></tbody>
                  本文介紹了當(dāng)同一模型也存在 HasMany 關(guān)系時(shí),如何更新 HasOne 關(guān)系?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我正在嘗試在 Eloquent 中定義相同的兩個(gè)模型之間的 HasMany 和 HasOne 關(guān)系.

                  我的Organization類有很多Contact:

                  公共函數(shù)contacts(){返回 $this->hasMany(Contact::class);}

                  同樣,我的 Contact 類反映了這種關(guān)系:

                  公共函數(shù)組織(){返回 $this->belongsTo(Organization::class);}

                  而且,每個(gè)組織都有一個(gè)主要"聯(lián)系人.我正在使用表列 organizations.primary_contact_id 來確定哪個(gè):

                  公共函數(shù)primaryContact(){返回 $this->hasOne(Contact::class, 'id', 'primary_contact_id');}

                  從這里開始,我被卡住了.Contact 中的反向關(guān)系已經(jīng)存在,所以我寫了另一個(gè)我認(rèn)為可以解決問題的函數(shù),計(jì)算如果我更新了父表中的值,Eloquent 自然會(huì)在contacts 表中獲取相應(yīng)的記錄,因?yàn)槲叶x了關(guān)系:

                  /*** @param AppContact*/公共函數(shù) setPrimaryContact($contact){$this->primary_contact_id = $contact->id;$this->save;}

                  但它沒有:

                  <預(yù)><代碼>>>>$org = 組織::查找(17)=>應(yīng)用組織 {#2923編號(hào):17,name: "測試組織",primary_contact_id: 33,}>>>$alice= $org->primaryContact=>應(yīng)用聯(lián)系{#2938編號(hào):33,組織 ID:17,fname: "愛麗絲",lname: "方丈",}>>>$bob = 聯(lián)系人::查找(34)=>應(yīng)用聯(lián)系{#2939編號(hào):34,組織 ID:17,fname: "鮑勃",lname: "面包師",}>>>$org->setPrimaryContact($bob)=>空值>>>$org=>應(yīng)用組織 {#2923編號(hào):17,name: "測試組織",primary_contact_id: 34,主要聯(lián)系人:AppContact {#2938編號(hào):33,組織 ID:17,fname: "愛麗絲",lname: "方丈",},}

                  您可以看到 setPrimaryContact($bob) 執(zhí)行得很好,因?yàn)?primary_contact_id 已更新為 Bob 的 id,但是 primaryContact 仍然列出 Alice.

                  為什么 primaryContact 沒有返回正確的對(duì)象?

                  解決方案

                  • 您的 setPrimaryContact 方法不會(huì)更新您的表,因?yàn)槟{(diào)用的是 $this->save,而不是 $this->save(), save 是一個(gè)方法
                  • $org->setPrimaryContact($bob)之后,你應(yīng)該調(diào)用$org->primaryContact->refresh() 以獲取更新的記錄.

                  I'm trying to define both a HasMany and HasOne relationship between the same two models in Eloquent.

                  My Organization class has many Contacts:

                  public function contacts()
                  {
                      return $this->hasMany(Contact::class);
                  }
                  

                  And likewise, my Contact class reflects this relationship:

                  public function organization()
                  {
                      return $this->belongsTo(Organization::class);
                  }
                  

                  But also, each Organization has exactly one "primary" Contact. I am using a table column organizations.primary_contact_id to identify which one:

                  public function primaryContact()
                  {
                      return $this->hasOne(Contact::class, 'id', 'primary_contact_id');
                  }
                  

                  From here, I'm stuck. The reverse relationship in Contact already exists, so I wrote another function I thought would do the trick, figuring if I updated the value in the parent table, Eloquent would naturally fetch the corresponding record in the contacts table since I defined the relationship:

                  /**
                   * @param AppContact
                   */
                  public function setPrimaryContact($contact)
                  {
                      $this->primary_contact_id = $contact->id;
                      $this->save;
                  }
                  

                  But it doesn't:

                  >>> $org = Organization::find(17)
                  => AppOrganization {#2923
                       id: 17,
                       name: "Test Org",
                       primary_contact_id: 33,
                     }
                  >>> $alice= $org->primaryContact
                  => AppContact {#2938
                       id: 33,
                       organization_id: 17,
                       fname: "Alice",
                       lname: "Abbot",
                     }
                  >>> $bob = Contact::find(34)
                  => AppContact {#2939
                       id: 34,
                       organization_id: 17,
                       fname: "Bob",
                       lname: "Baker",
                     }
                  >>> $org->setPrimaryContact($bob)
                  => null
                  >>> $org
                  => AppOrganization {#2923
                       id: 17,
                       name: "Test Org",
                       primary_contact_id: 34,
                       primaryContact: AppContact {#2938
                         id: 33,
                         organization_id: 17,
                         fname: "Alice",
                         lname: "Abbot",
                       },
                     }
                  

                  You can see setPrimaryContact($bob) executed fine, as primary_contact_id got updated to Bob's id, but primaryContact still lists Alice.

                  Why is primaryContact not returning the correct object?

                  解決方案

                  • Your setPrimaryContact method won't update your table, because you call $this->save, not $this->save(), save is a method
                  • After $org->setPrimaryContact($bob), you should call $org-> primaryContact->refresh() to get the updated record.

                  這篇關(guān)于當(dāng)同一模型也存在 HasMany 關(guān)系時(shí),如何更新 HasOne 關(guān)系?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個(gè)服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識(shí)別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個(gè)參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問被拒絕)
                    <tbody id='4ikJU'></tbody>

                    <bdo id='4ikJU'></bdo><ul id='4ikJU'></ul>

                      <i id='4ikJU'><tr id='4ikJU'><dt id='4ikJU'><q id='4ikJU'><span id='4ikJU'><b id='4ikJU'><form id='4ikJU'><ins id='4ikJU'></ins><ul id='4ikJU'></ul><sub id='4ikJU'></sub></form><legend id='4ikJU'></legend><bdo id='4ikJU'><pre id='4ikJU'><center id='4ikJU'></center></pre></bdo></b><th id='4ikJU'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4ikJU'><tfoot id='4ikJU'></tfoot><dl id='4ikJU'><fieldset id='4ikJU'></fieldset></dl></div>
                        • <legend id='4ikJU'><style id='4ikJU'><dir id='4ikJU'><q id='4ikJU'></q></dir></style></legend>

                            <small id='4ikJU'></small><noframes id='4ikJU'>

                            <tfoot id='4ikJU'></tfoot>
                            主站蜘蛛池模板: 久久久久久久亚洲精品 | 中文字幕在线精品 | 久久久久久91| 亚洲一区影院 | 亚洲国产精品一区二区久久 | 欧美中文在线 | 欧美日韩精品中文字幕 | 亚洲av一级毛片 | 韩国欧洲一级毛片 | 国产精品久久免费观看 | 中文字幕一区二区三区不卡在线 | 亚洲成人网在线 | 日韩电影a| 中文字幕免费中文 | 亚洲精品一区av在线播放 | 天天干国产 | 亚洲精品久久久久久久久久久久久 | 日韩在线观看一区二区三区 | 亚洲精品第一 | 大学生a级毛片免费视频 | 风间由美一区二区三区在线观看 | aaaa网站 | 日韩在线h| 免费看黄色国产 | 精品久久国产 | 欧美三级在线 | 国产91在线 | 中日 | 在线国产视频 | 久久久国产一区二区三区 | 色婷婷国产精品综合在线观看 | 视频在线观看一区二区 | 婷婷在线免费 | www.日日操 | 欧美日韩视频在线播放 | 亚洲欧美少妇 | 亚洲精品永久免费 | 成人在线观看网址 | 成人一级片在线观看 | 国产高清在线观看 | 一区二区中文 | 在线观看日本网站 |