有两个表:play表和type表
play表字段:
id type
type表字段:
id title
立即学习“PHP免费学习笔记(深入)”;
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 模型对
象的字段属性冲突。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号