PHP关系查询

时间:2015-11-02 15:01:59

标签: php codeigniter relationship

我正在使用PHP& jamierumbelow /笨基模型 我有两张桌子

Table 1: Class
classId  classStd
-----------------------
1         Prep
3         NUR
4         STD-1
5         STD-2

Table2: Section
sectionId  classId  section  sectionName
---------------------------------------------------
5            1       A         rose
6            1       B         red
7            1       C         green
8            3       A         ROME
9            3       B         PARIS

关系是:一个班级可以有很多部分。 我的功能

function section_get(){

$this->load->model('Model_section');
// this return all section with its classSTD name correctly
$pages = $this->Model_section->with('class')->get_all();
$this->response($pages);

}

返回以下回复

[
    {"sectionId":"5","classId":"1","section":"A","sectionName":"rose","class":{"classId":"1","classStd":"Prep"}},
    {"sectionId":"6","classId":"1","section":"B","sectionName":"red","class":{"classId":"1","classStd":"Prep"}},
    {"sectionId":"7","classId":"1","section":"C","sectionName":"green","class":{"classId":"1","classStd":"Prep"}},
    {"sectionId":"8","classId":"3","section":"A","sectionName":"ROME","class":{"classId":"3","classStd":"NUR"}},
    {"sectionId":"9","classId":"3","section":"B","sectionName":"PARIS","class":{"classId":"3","classStd":"NUR"}}
]

我如何才能获得特定记录,例如“classId = 1”

2 个答案:

答案 0 :(得分:0)

使用get_many_by方法:

$pages = $this->Model_section->with('class')->get_many_by('classId',1);

答案 1 :(得分:0)

或者您可以执行以下操作,创建一个带有连接的dB查询,该连接获取您需要的所有信息,然后按groupID对结果进行分组