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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # hasOne(多對一關系) > 譯者:[飛龍](https://github.com/wizardforcel) > 來源:[hasOne](https://github.com/dresende/node-orm2/wiki/hasOne) hasOne關聯是一種多對一的關系,意思是你定義的模型可以有多個實例指向一個其它的實例(所屬相同模型或不同模型)。 ## 用法 ``` Animal.hasOne(association_name [, association_model [, options ] ]); ``` ## 描述 + `association_name`是兩個模型之間的關系名稱‘ + `association_model`是要關聯的另一個模型(如果沒有定義,假設為同一個模型,大多數情況下這可能不是你想要的); + `options`是一個對象,擁有一些和關聯有關的,你可以調整的屬性,比如自動獲取,再比如表(SQL中)或者集合(MongoDB中)的名稱。 ## 示例 ``` Animal.hasOne("owner", Person); ``` 在背后,這條語句意思是`Animal`集合擁有一個屬性`owner_id`(這個名稱可以通過選項來修改,`{field: 'ownerid'}`),它會指向`Person`集合的某個人。如果關聯并不是必須的,則可以為空。 這個關聯也會創建一些額外的便利方法(叫做關聯訪問器)來幫助你管理它。訪問器的名稱也可以修改(同上,在選項里面),默認情況下,它們會擁有和關聯名稱相似的名稱。例如,下面的代碼展示了可以做類似這樣的事情: ``` // assuming John is a Person.. Animal.find({ name: "Deco" }).first(function (err, Deco) { Deco.setOwner(John, function (err) { // John is now the owner of Deco }); }); ``` 其它的訪問器: + `getOwner(callback)` - 獲取關聯的所有者 + `hasOwner(callback)` - (在回調中)返回這個動物是否擁有所有者 + `removeOwner(callback)` - 移除和所有者的關聯關系(如果存在的話) ## 關聯反轉 有時你希望通過對面的模型來訪問關聯。在上面的例子中,是通過`Person`。你可以向關聯傳遞一個選項來實現它。 ``` Animal.hasOne('owner', Person, { reverse: "pets" }); ``` 之后,每個`person`實例都有有兩個便利方法: + `getPets(callback)` - 獲取所有和這個人有關聯的動物 + `setPets(cat, dog, callback)` - 移除所有和這個人有關聯的動物,并且添加貓和狗
                  <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>

                              哎呀哎呀视频在线观看