<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 關聯操作 ### 介紹 數據庫表通常相互關聯。例如,博客文章可能有很多評論,或者訂單可能與放置它的用戶有關。October使管理和使用這些關系變得容易,并支持幾種不同類型的關系。 > **注:**如果您在查詢中選擇特定的列和要加載的關系為好,你需要確保做出包含密鑰數據(即列`id`,`foreign_key`等等)都包含在你的SELECT語句。否則,十月無法建立聯系。 ### [](https://octobercms.com/docs/database/relations#relationship-types)關系類型 可以使用以下關系類型: * [一對一](https://octobercms.com/docs/database/relations#one-to-one) * [一對多](https://octobercms.com/docs/database/relations#one-to-many) * [多對多](https://octobercms.com/docs/database/relations#many-to-many) * [有很多通過](https://octobercms.com/docs/database/relations#has-many-through) * [多態關系](https://octobercms.com/docs/database/relations#polymorphic-relations) * [多對多態關系](https://octobercms.com/docs/database/relations#many-to-many-polymorphic-relations) ### [](https://octobercms.com/docs/database/relations#deferred-binding)延遲綁定 延遲綁定允許您推遲模型關系的綁定,直到主記錄提交更改為止。如果您需要準備一些模型(例如文件上傳)并將它們與尚不存在的其他模型相關聯,則這特別有用。 您可以使用**會話密鑰將**任何數量的**從屬**模型相對于**主**模型進行延遲。當主記錄與會話密鑰一起保存時,與從屬記錄的關系將自動為您更新。后端[Form行為](https://octobercms.com/docs/backend/form)自動支持延遲綁定,但是您可能希望在其他地方使用此功能。[](https://octobercms.com/docs/backend/form) ### [](https://octobercms.com/docs/database/relations#deferred-session-key)生成會話密鑰 會話密鑰對于延遲綁定是必需的。您可以將會話密鑰視為事務標識符。相同的會話密鑰應用于綁定/解除綁定關系并保存主模型。您可以使用PHP`uniqid()`函數生成會話密鑰。請注意,[表單幫助程序](https://octobercms.com/docs/cms/markup#forms)會自動生成一個包含會話密鑰的隱藏字段。 ~~~ $sessionKey = uniqid('session_key', true); ~~~ ### [](https://octobercms.com/docs/database/relations#defer-binding)推遲關系綁定 除非保存該帖子,否則下一個示例中的評論將不會添加到該帖子中。 ~~~ $comment = new Comment; $comment->content = "Hello world!"; $comment->save(); $post = new Post; $post->comments()->add($comment, $sessionKey); ~~~ > **注意**:該`$post`對象尚未保存,但是如果保存發生關系將被創建。 ### [](https://octobercms.com/docs/database/relations#defer-unbinding)推遲解除關系 除非保存該帖子,否則下一個示例中的評論將不會被刪除。 ~~~ $comment = Comment::find(1); $post = Post::find(1); $post->comments()->remove($comment, $sessionKey); ~~~ ### [](https://octobercms.com/docs/database/relations#list-all-bindings)列出所有綁定 使用`withDeferred`關系的方法加載所有記錄,包括延遲的。結果還將包括現有關系。 ~~~ $post->comments()->withDeferred($sessionKey)->get(); ~~~ ### [](https://octobercms.com/docs/database/relations#cancel-all-bindings)取消所有綁定 取消延遲綁定并刪除從屬對象而不是將它們保留為孤立對象是個好主意。 ~~~ $post->cancelDeferred($sessionKey); ~~~ ### [](https://octobercms.com/docs/database/relations#commit-all-bindings)提交所有綁定 保存主模型時,可以通過為會話密鑰提供方法的第二個參數來提交(綁定或取消綁定)所有延遲的綁定`save`。 ~~~ $post = new Post; $post->title = "First blog post"; $post->save(null, $sessionKey); ~~~ 相同的方法適用于模型的`create`方法: ~~~ $post = Post::create(['title' => 'First blog post'], $sessionKey); ~~~ ### [](https://octobercms.com/docs/database/relations#lazily-commit-bindings)延遲提交綁定 如果`$sessionKey`在保存時無法提供,則可以使用以下代碼隨時提交綁定: ~~~ $post->commitDeferred($sessionKey); ~~~ ### [](https://octobercms.com/docs/database/relations#cleanup-bindings)清理孤立的綁定 銷毀所有尚未提交且早于1天的綁定: ~~~ October\Rain\Database\Models\DeferredBinding::cleanUp(1); ~~~ > **注意:**十月會自動銷毀早于5天的延遲綁定。后端用戶登錄系統時會發生這種情況。 ### [](https://octobercms.com/docs/database/relations#disable-deferred-binding)禁用延遲綁定 有時您可能需要完全禁用給定模型的延遲綁定,例如,如果要從單獨的數據庫連接中加載它。為此,您需要確保在運行內部save方法中的遞延綁定鉤子之前和之后,模型的`sessionKey`屬性是正確`null`的。為此,您可以綁定到模型的`model.saveInternal`事件: ~~~ public function __construct() { $result = parent::__construct(...func_get_args()); $this->bindEvent('model.saveInternal', function () { $this->sessionKey = null; }); return $result; } ~~~ > **注意:**這將完全禁用對您應用此替代的任何模型的延遲綁定。
                  <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>

                              哎呀哎呀视频在线观看