tp5模型belongsTo和hasOne的區別

2021-08-06 06:33:06 字數 294 閱讀 1720

在使用tp5模型的orm的時候出現belongsto和hasone都有表示一對一的關係,但是二者並不相同。以下舉例說明兩者的區別:

首先有user表 字段 id name password欄位

然後有user_address表 id user_id city欄位

在user模型中關聯user_address表的時候使用hasone,因為在user表中沒有關聯兩個表的外來鍵

在useraddress模型中關聯user表的時候使用belongsto,因為在user_address表中有關聯兩個表的外來鍵user_id

TP5中belongsTo跟hasOne的區別

在使用tp5模型的orm的時候出現belongsto和hasone都有表示一對一的關係,但是二者並不相同。以下舉例說明兩者的區別 首先有user表 字段 id name password欄位 然後有user address表 id user id city欄位 在user模型中關聯user addr...

tp5 模型關聯

二 文章中用到的表結構 image 表,儲存的位置資訊 banner 推薦位表,儲存推薦位的型別 banner item 表,推薦位中的資訊條目,可以看到它擁有外來鍵 img id theme 表,商品活動主題,包含頭圖,主題圖 product 表,商品表 theme product 表,theme...

TP5使用模型查詢資料

前提 引入model 1.get 方法,引數為 主鍵值 res user get 1 toarray 方法是將 get的值,轉為陣列 res res toarray get 方法還支援閉包 res user get function query res res toarray dump res 2....