2016-05-23 4 views
1

私は3つのテーブル、つまりcelebs,celeb_rolesceleb_industryを持っています。
celebsは、他の2つのテーブルと1対多の関係を持つメインテーブルです。モデルからの多対多リレーションシップとビューの解析

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(); 
} 

同じceleb_idが他の2つのテーブルのそれぞれに2つのレコードを有するcelebsにおける1つのレコードが、存在します。 私が得た結果は以下の通りです。

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テーブルの1つのレコードのみのために、私は私が過負荷の少しだと思うれ、ループ内でこれを配置する必要があります。他の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 get queryを使用すると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ではなく、2つのセレブの役割と2つの業界があるので、私は4を得ています。しかし、私はarray_uniqueを使用して必要な結果を得ることができると思います。しかし、大きな方法を示してくれてありがとう。 – user1638279

関連する問題