tp5 1 多對多關聯,新增中間表自動時間戳

2022-02-28 11:37:30 字數 692 閱讀 2560

think 5.1 多對多關聯,有兩種方式:

方式一:

直接寫中間表名,進行關聯

格式:

return $this->belongstomany('關聯模型名', '中間表名', '中間表外來鍵,對應關聯模型主鍵', '當前模型關聯鍵,中間表中的字段對應當前模型的主鍵');
例:

return $this->belongstomany('templatemodel', 'user_template_related', 'template_id', 'user_id');
因為,中間表模型的基類pivot預設關閉了時間戳自動寫入,所以我們需要使用使用第二種方式

方式二:

建立中間表模型,進行關聯

例:1.建立中間表模型

<?php

use think\model\pivot;

class usertemplatepivotmodel extends pivot

2.使用關聯

格式:

return $this->belongstomany('關聯模型名', '中間表模型,要帶命名空間', '中間表外來鍵,對應關聯模型主鍵', '當前模型關聯鍵,中間表中的字段對應當前模型的主鍵');
例:

thinkphp5多對多關聯,中間表進行多型關聯

這是中間表,不要問我為什麼這麼設計,這是領導要求的 create table at relation table id int 11 not null auto increment,aid int 11 not null comment aid bid int 11 not null comment...

hibernate 多對多關係中間屬性關聯方法

hibernate 框架可以將面向關係的資料庫模型封裝成物件導向的資料庫模型,這樣以來一方面了我們在具體應用中的資料庫設計,另一方面又簡化了我們的編碼。然而在現實中我們往往會發現有些面向關係的資料庫模型轉化成物件導向的資料庫模型之後很難再 hibernate 中配置。比如 在多對多關係模型中,中間表...

TP6模型多對多關聯查詢

user模型 class user extends model 多對多關聯查詢 user表查詢條件 map role表查詢條件 where data user with roles function query use where where map select 或 user表查詢條件 map r...