如何解決關(guān)于thinkphp模型中hasOne中字段同名的問題
來源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 20:12:53
如何解決關(guān)于thinkphp模型中hasOne中字段同名的問題
如何解決關(guān)于thinkphp模型中hasOne中字段同名的問題:有兩個(gè)表:play表和type表play表字段:id typetype表字段:id titleplay表中的type和type表中的id進(jìn)行關(guān)聯(lián)。thinkphp5中的模型定義如下:play模型:class Play extends Model { protected $table = 'wx_play'
導(dǎo)讀如何解決關(guān)于thinkphp模型中hasOne中字段同名的問題:有兩個(gè)表:play表和type表play表字段:id typetype表字段:id titleplay表中的type和type表中的id進(jìn)行關(guān)聯(lián)。thinkphp5中的模型定義如下:play模型:class Play extends Model { protected $table = 'wx_play'
有兩個(gè)表:play表和type表
play表字段:
id type
type表字段:
id title
play表中的type和type表中的id進(jìn)行關(guān)聯(lián)。
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函數(shù),不能寫成type,不然會(huì)跟play表中的type字段沖突,導(dǎo)致只查詢到play表中的字段,而不是type表的對(duì)象。
根據(jù)thinkphp5的文檔說明:
提示:
Play模型的 type2 方法就是一個(gè)關(guān)聯(lián)定義方法,方法名可以隨意命名,但注意要避免和Play 模型對(duì)
象的字段屬性沖突。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
如何解決關(guān)于thinkphp模型中hasOne中字段同名的問題
如何解決關(guān)于thinkphp模型中hasOne中字段同名的問題:有兩個(gè)表:play表和type表play表字段:id typetype表字段:id titleplay表中的type和type表中的id進(jìn)行關(guān)聯(lián)。thinkphp5中的模型定義如下:play模型:class Play extends Model { protected $table = 'wx_play'