有两个表: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 模型对
象的字段属性冲突。
以上就是如何解决关于thinkphp模型中hasone中字段同名的问题的详细内容。