2016-05-23 128 views
1

我有3個表格 - celebs,celeb_roles,celeb_industry
celebs是與其他2個表有一對多關係的主表。Codeigniter從模型中獲取一對多關係

celebs是具有列 - ID,名稱,國籍,地位。

celebs_roles具有列 - id,celeb_id,role_id。

celebs_industry有欄 - id,celeb_id,industry_id。

有限和固定的角色和行業數量。所以我已經把它們放在配對文件id=>value中。所以如果從結果中得到role_id,我可以從配置文件中獲取值。 industry_id也一樣。

現在爲了從一個給定的id獲取名人詳情,我已經在模型中寫下了如下的查詢。

public function getCelebSingle($celebId) 
{ 
    $this->db->select('*'); 
    $this->db->from('celebs'); 
    $this->db->join('celebs_roles', 'celebs_roles.celeb_id = celebs.id'); 
    $this->db->join('celebs_industry', 'celebs_industry.celeb_id = celebs.id'); 
    $this->db->where('celebs_roles.celeb_id', $celebId); 
    $this->db->where('celebs_industry.celeb_id', $celebId); 
    $query = $this->db->get(); 
    return $query->result_array(); 
} 

在數據庫存在celebs一個記錄,與具有2個記錄在相互2代表的同一celeb_id。 我得到的結果如下。

Array 
(
    [0] => Array 
     (
     [id] => 1 
     [name] => new test 
     [nationality] => whatever 
     [status] => Pending 
     [celeb_id] => 1 
     [role_id] => 1 
     [industry_id] => 1 
    ) 

[1] => Array 
    (
     [id] => 1 
     [name] => new test 
     [nationality] => whatever 
     [status] => Pending 
     [celeb_id] => 1 
     [role_id] => 3 
     [industry_id] => 1 
    ) 

[2] => Array 
    (
     [id] => 2 
     [name] => new test 
     [nationality] => whatever 
     [status] => Pending 
     [celeb_id] => 1 
     [role_id] => 1 
     [industry_id] => 2 
    ) 

[3] => Array 
    (
     [id] => 2 
     [name] => new test 
     [nationality] => whatever 
     [status] => Pending 
     [celeb_id] => 1 
     [role_id] => 3 
     [industry_id] => 2 
    ) 
) 

所以只有一個celebs表中的記錄,我需要把這個在一個循環,我認爲這是一個有點超負荷。如果其他2個表中的記錄數增加,則結果數組中的項也會增加。所以我想知道,我能得到如下結果嗎?

Array 
(
    [0] => Array 
    (
     [id] => 1 
     [name] => new test 
     [nationality] => whatever 
     [status] => Pending 
     [celeb_roles] => array(
           [0] => Array 
           (
            [id] => 1 
            [celeb_id] => 1 
            [role_id] => 1 
           ) 
           [0] => Array 
           (
            [id] => 1 
            [celeb_id] => 1 
            [role_id] => 2 
           ) 
         ) 
     [celeb_industry] => array(
           [0] => Array 
           (
            [id] => 1 
            [celeb_id] => 1 
            [industry_id] => 1 
           ) 
           [0] => Array 
           (
            [id] => 1 
            [celeb_id] => 1 
            [industry_id] => 2 
           ) 
         ) 
    ) 
) 

這只是對結果數組的期望,但我不知道如何實現這個結果數組。有人可以看看嗎?

這是一個可能的重複 - Codeigniter group by and create multidimensional array。但答案並不令人滿意。答案明確指出不使用JOIN,但使用正常的Codeigniter獲取查詢,我認爲這將導致3個查詢的問題。如果我選擇celebs表的所有記錄怎麼辦?然後,查詢的數量將是3 * Number of records in celebs table,這將超載。如果沒有其他辦法,我只能這樣做,但我希望有更好的方法來做到這一點。

+0

可能重複:http://stackoverflow.com/questions/18745345/codeigniter-group-by-and-create-multidimensional-array – Sebastianb

回答

0

請做到這一點,我不確定這是否正確,但如果您想按預期排列的方式聲明結果,則需要嘗試這種方法。

$this->db->select('*'); 
    $this->db->from('celebs'); 
    $this->db->join('celebs_roles', 'celebs_roles.celeb_id = celebs.id'); 
    $this->db->join('celebs_industry', 'celebs_industry.celeb_id = celebs.id'); 
    $this->db->where('celebs_roles.celeb_id', $celebId); 
    $this->db->where('celebs_industry.celeb_id', $celebId); 
    $query = $this->db->get(); 
    $result = $query->result_array(); 
    $new_array = array(); 
    foreach ($result as $key => $value){ 
     $new_array = $value; 
      foreach ($result as $key1 => $value1){ 
       $new_array['celeb_roles'][$key1]['id'] = $value1['id']; 
       $new_array['celeb_roles'][$key1]['celeb_id'] = $value1['celeb_id']; 
       $new_array['celeb_roles'][$key1]['role_id'] = $value1['role_id']; 
      } 

    } 
+0

真的很感謝你的幫助。我試過這個。現在celeb_roles經過一些修改後顯示在一個陣列中,但不是2個celeb_roles,我得到4個,因爲有2個名人角色和2個行業。但我想我可以使用array_unique並獲得所需的結果。但是非常感謝您展示道路。 – user1638279