如何解決關于thinkphp模型中hasOne中字段同名的問題
來源:懂視網
責編:小采
時間:2020-11-27 20:12:53
如何解決關于thinkphp模型中hasOne中字段同名的問題
如何解決關于thinkphp模型中hasOne中字段同名的問題:有兩個表:play表和type表play表字段:id typetype表字段:id titleplay表中的type和type表中的id進行關聯。thinkphp5中的模型定義如下:play模型:class Play extends Model { protected $table = 'wx_play'
導讀如何解決關于thinkphp模型中hasOne中字段同名的問題:有兩個表:play表和type表play表字段:id typetype表字段:id titleplay表中的type和type表中的id進行關聯。thinkphp5中的模型定義如下:play模型:class Play extends Model { protected $table = 'wx_play'

有兩個表:play表和type表
play表字段:
id type
type表字段:
id title
play表中的type和type表中的id進行關聯。
thinkphp5中的模型定義如下:
play模型:
class Play extends Model
{
protected $table = 'wx_play';
public function type2()
{
return $this->hasOne("Type", "id", "type");
}
type模型:
class Type extends Model
{
protected $table = 'wx_type';
}
注意:
play模型中的type2函數,不能寫成type,不然會跟play表中的type字段沖突,導致只查詢到play表中的字段,而不是type表的對象。
根據thinkphp5的文檔說明:
提示:
Play模型的 type2 方法就是一個關聯定義方法,方法名可以隨意命名,但注意要避免和Play 模型對
象的字段屬性沖突。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
如何解決關于thinkphp模型中hasOne中字段同名的問題
如何解決關于thinkphp模型中hasOne中字段同名的問題:有兩個表:play表和type表play表字段:id typetype表字段:id titleplay表中的type和type表中的id進行關聯。thinkphp5中的模型定義如下:play模型:class Play extends Model { protected $table = 'wx_play'