2014-01-31 11 views
16

beitreten Ich verwende Codeigniter-Framework, um eine Musik cms zu entwickeln. Ich habe 3 Tabellen in Mysql Datenbank, Derzeit arbeite ich in "Album" Tabelle und "Model, Controller". Ich möchte "Album" Tabelle 1 und JOIN "Album" -> "cat_id" mit "Kategorie" -> "cat_id" auswählen und alle Kategorien Datensätze abrufen.Wie man drei Tabellen in Codeigniter

dann will ich "Album" JOIN -> "album_id" auf "Soundtrack" -> "album_id" dann alle Soundtrack holen aufzeichnet A bis Z.

Bitte jemand mir helfen, die richtige codeigniter Abfrage zu zeigen, wie Ich kann SELECT und JOIN Tabellen dann Datensätze aus 3 Tabellen abrufen,?

Tabelle 1 -> Kategorie

  • cat_id

  • CAT_NAME

  • cat_title
  • Datum

    Tabelle 2 -> Album

  • cat_id
  • album_id
  • ALBUM_TITLE

  • album_details

    Tabelle 3 -> Soundtrack

  • album_id

  • track_title

  • track_url
  • Datum
+0

Sehen Sie sich diesen Link http://ellislab.com/codeigniter/user-guide/database/active_record.html an.Hier erfahren Sie, wie Sie Tabellen in Codeigniter verknüpfen. – JazyK

+0

Zeigen Sie uns einen Code von dem, was Sie versucht haben – machineaddict

Antwort

32

Mit diesem Code in Modell

public function funcname($id) 
{ 
    $this->db->select('*'); 
    $this->db->from('Album a'); 
    $this->db->join('Category b', 'b.cat_id=a.cat_id', 'left'); 
    $this->db->join('Soundtrack c', 'c.album_id=a.album_id', 'left'); 
    $this->db->where('c.album_id',$id); 
    $this->db->order_by('c.track_title','asc');   
    $query = $this->db->get(); 
    if($query->num_rows() != 0) 
    { 
     return $query->result_array(); 
    } 
    else 
    { 
     return false; 
    } 
} 
+1

und wo ich $ ID übergeben ich meine album_id? um spezifische Aufzeichnungen zu bekommen? –

+0

Bitte verwenden Sie den obigen Code .. –

+1

Wichtig, wenn Sie mytyable.id und mysecondtable.id haben und Sie wählen ('*'), wird das erste ID-Feld nicht angezeigt. Sie müssen manuell auswählen ('mytable.id als id1, mysecondtable.id als id2, mytable. *, Mysecondable. *') ... codeigniter-Joins enthalten das Tabellenpräfix nicht, sodass das Recordset keine doppelten Felder zurückgibt. – ssaltman

5

versuchen, diese

In Ihrem Modell

Wenn u wollen, dass alle Album bekommen Daten verwenden

function get_all_album_data() { 

    $this->db->select ('*'); 
    $this->db->from ('Album'); 
    $this->db->join ('Category', 'Category.cat_id = Album.cat_id' , 'left'); 
    $this->db->join ('Soundtrack', 'Soundtrack.album_id = Album.album_id' , 'left'); 
    $query = $this->db->get(); 
    return $query->result(); 
} 

wenn u Verwendung bestimmtes Album Daten

function get_album_data($album_id) { 

    $this->db->select ('*'); 
    $this->db->from ('Album'); 
    $this->db->join ('Category', 'Category.cat_id = Album.cat_id' , 'left'); 
    $this->db->join ('Soundtrack', 'Soundtrack.album_id = Album.album_id' , 'left'); 
    $this->db->where ('Album.album_id', $album_id); 
    $query = $this->db->get(); 
    return $query->result(); 
} 
3

Versuchen Sie dieses für Daten ...

function get_album_data() { 
    $this->db->select ('album.*,cat.*,s_track.*') 
     ->from ('albums as album'); 
     ->join ('categories cat', 'cat.cat_id = album.cat_id') 
     ->join ('soundtracks s_tracks ', 's_tracks.album_id = album.album_id'); 
    $query = $this->db->get(); 
    return $query->result(); 
} 

während für Datum zu erhalten versuchen, diese ...

function get_album_datum($album_id) { 
    $this->db->select ('album.*,cat.*,s_track.*') 
     ->from ('albums as album'); 
     ->join ('categories cat', 'cat.cat_id = album.cat_id') 
     ->join ('soundtracks s_tracks ', 's_tracks.album_id = album.album_id'); 
    $this->db->where ('album.album_id', $album_id); 
    $query = $this->db->get(); 
    return $query->row(); 
} 
0
Check bellow code it`s working fine and common model function also 
    supported more then one join and also supported multiple where condition 
order by ,limit.it`s EASY TO USE and REMOVE CODE REDUNDANCY. 

    ================================================================ 
    *Album.php 
    //put bellow code in your controller 
    ================================================================= 

    $album_id='';//album id 

    //pass join table value in bellow format 
    $join_str[0]['table'] = 'Category'; 
    $join_str[0]['join_table_id'] = 'Category.cat_id'; 
    $join_str[0]['from_table_id'] = 'Album.cat_id'; 
    $join_str[0]['join_type'] = 'left'; 

    $join_str[1]['table'] = 'Soundtrack'; 
    $join_str[1]['join_table_id'] = 'Soundtrack.album_id'; 
    $join_str[1]['from_table_id'] = 'Album.album_id'; 
    $join_str[1]['join_type'] = 'left'; 


    $selected = "Album.*,Category.cat_name,Category.cat_title,Soundtrack.track_title,Soundtrack.track_url"; 

    $albumData= $this->common->select_data_by_condition('Album', array('Soundtrack.album_id' => $album_id), $selected, '', '', '', '', $join_str); 
           //call common model function 

    if (!empty($albumData)) { 
     print_r($albumData); // print album data 
    } 

========================================================================= 
    Common.php 
    //put bellow code in your common model file 
======================================================================== 

    function select_data_by_condition($tablename, $condition_array = array(), $data = '*', $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str = array()) { 
    $this->db->select($data); 
    //if join_str array is not empty then implement the join query 
    if (!empty($join_str)) { 
     foreach ($join_str as $join) { 
      if ($join['join_type'] == '') { 
       $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id']); 
      } else { 
       $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id'], $join['join_type']); 
      } 
     } 
    } 

    //condition array pass to where condition 
    $this->db->where($condition_array); 


    //Setting Limit for Paging 
    if ($limit != '' && $offset == 0) { 
     $this->db->limit($limit); 
    } else if ($limit != '' && $offset != 0) { 
     $this->db->limit($limit, $offset); 
    } 
    //order by query 
    if ($sortby != '' && $orderby != '') { 
     $this->db->order_by($sortby, $orderby); 
    } 

    $query = $this->db->get($tablename); 
    //if limit is empty then returns total count 
    if ($limit == '') { 
     $query->num_rows(); 
    } 
    //if limit is not empty then return result array 
    return $query->result_array(); 
}