当前位置:Gxlcms > PHP教程 > thinkphp+mysql中出现的关联模型问题?

thinkphp+mysql中出现的关联模型问题?

时间:2021-07-01 10:21:17 帮助过:4人阅读

我定义了一张关系表MemberSchool,里面存放的字段有college_id,senior_id,major_id
还有另外三张表college,senior,major表
这是我原先用查询语言的代码

$memberSchool = M('Memberschool');
$where['member_id'] = array('EQ',$this->member_id);
$result = $memberSchool->field('college_name,major_name,school_name')
            ->join('JOIN __COLLEGE__ ON
               __MEMBERSCHOOL__.college_id = __COLLEGE__.college_id')
            ->join('JOIN __SENIOR__ ON
               __MEMBERSCHOOL__.senior_id = __SENIOR__.id')
            ->join('JOIN __MAJOR__ ON
            __MEMBERSCHOOL__.major_id = __MAJOR__.major_id')
            ->where($where)
            ->find();
        return $result;

现在我想利用关联模型的语句把他们的数据查询出来,请问要怎么查询呢?要以哪个表作为参照表呢?

回复内容:

我定义了一张关系表MemberSchool,里面存放的字段有college_id,senior_id,major_id
还有另外三张表college,senior,major表
这是我原先用查询语言的代码

$memberSchool = M('Memberschool');
$where['member_id'] = array('EQ',$this->member_id);
$result = $memberSchool->field('college_name,major_name,school_name')
            ->join('JOIN __COLLEGE__ ON
               __MEMBERSCHOOL__.college_id = __COLLEGE__.college_id')
            ->join('JOIN __SENIOR__ ON
               __MEMBERSCHOOL__.senior_id = __SENIOR__.id')
            ->join('JOIN __MAJOR__ ON
            __MEMBERSCHOOL__.major_id = __MAJOR__.major_id')
            ->where($where)
            ->find();
        return $result;

现在我想利用关联模型的语句把他们的数据查询出来,请问要怎么查询呢?要以哪个表作为参照表呢?

人气教程排行