TP3 多对多关联查询

106 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

image.png

场景

一个学生可以选修多门课程,一个课程可以被多个学生选修,查询某个课程的信息及选修该课程的学生姓名列表或者查询某个学生的信息及该学生选修课程的名称列表。

数据模型

  • think_student 学生表
idstudent_name
1柯南
2元太
3步美
4小哀
5光彦
  • think_subject 课程表
idsubject_name
1算数
2图画
3体育
  • think_student_subject 学生课程表
idstudent_idsubject_id
111
232
313

多对多关联

  • 创建 SubjectModel.class.php 内容如下:
<?php
namespace Home\Model;
use Think\Model\RelationModel;
class SubjectModel extends RelationModel {
    protected $_link = array(
        'Student' => array(
            'mapping_type' =>  self::MANY_TO_MANY,
            'mapping_fields'    => 'student_name',
            'mapping_name'      =>  'Students',
            'foreign_key' =>  'subject_id',
            'relation_foreign_key' =>  'student_id',
            'relation_table' => 'think_student_subject',
        ),
    );
}
  • 创建 StudentModel.class.php 内容如下:
<?php
namespace Home\Model;
use Think\Model\RelationModel;
class StudentModel extends RelationModel {
    protected $_link = array(
        'Subject' => array(
            'mapping_type' =>  self::MANY_TO_MANY,
            'mapping_fields'    => 'subject_name',
            'mapping_name'      =>  'Subjects',
            'foreign_key' =>  'student_id',
            'relation_foreign_key' =>  'subject_id',
            'relation_table' => 'think_student_subject',
        ),
    );
}
  • 创建 TestController.class.php 内容如下:
<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller {
    public function manyToMany(){
		// 查询某个课程的信息及选修该课程的学生姓名列表
        $subject = D('Subject');
        $record = $subject->relation(Students)->find(3);
        echo json_encode($record);
		// 查询某个学生的信息及该学生选修课程的名称列表
		$student = D('Student');
        $record = $student->relation('Subjects')->find(1);
        echo json_encode($record);
    }
}

查询

{
    "id": "3",
    "subject_name": "体育",
    "students": [
        {
            "student_name": "光彦"
        },
        {
            "student_name": "柯南"
        }
    ]
}

查询体育课程的信息及选修该课程的学生姓名列表

{
    "id": "1",
    "student_name": "柯南",
    "Subjects": [
        {
            "subject_name": "算数"
        },
        {
            "subject_name": "体育"
        }
    ]
}

查询柯南学生的信息及该学生选修课程的名称列表