2015-11-02 135 views
0

我使用PHP & jamierumbelow /笨基模型 我有兩個表PHP關係查詢

 
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 

的關係是:一個類可以有多個部分。 我的功能

功能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 「

+0

這是否工作:'$頁= $ this-> Model_section-> with('class') - > get_many_by('classId',1);'? – prograhammer

回答

0

使用get_many_by方法:

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

感謝它的工作。有沒有什麼辦法可以獲得特定的字段,如select field1,field2而不是select * – fresher

+0

試試'$ this-> Model_section - > _ database-> select('field1,field2')'。 – prograhammer

+0

沒有多數民衆贊成在工作。我用<$this-> Model_section-> dropdown('sectionId','classId');但後來我無法觸發 - >('class') - > get_many_by('classId',3);與它 – fresher

0

或者你可以執行以下操作創建一個分貝查詢加入,讓你需要那麼所有信息組的結果通過組ID