简体   繁体   中英

codeigniter 3 join tables and get one record

I have a problem with SQL JOIN with Codeigniter 3. There are 3 tables in database.

Film table:

id    title                 image       ....
----  --------------------  ----------  
206   The Maltese Falcon    image-link  ..

Genres table:

genres_id  genres_name
---------  -----------
1          Crime
2          Drama

Film_genres table:

fg_id  film_id  genres_id
-----  -------  ---------
1      206      1
2      206      2

My model method for single film

public function getFilm($id){
    $this->db->select()
            ->from('film'); 
    $this->db->join('film_genres','film_genres.film_id = id','left');
    $this->db->join('genres','genres.genres_id = film_genres.genres_id','left');
    $this->db->where('id',$id);
    $this->db->group_by('id');
    $query = $this->db->get();
    $result = $query->result_array();
    return $result;

}

The out of code

Array
(
    [0] => Array
        (
            [id] => 206
            [title] => Malta Kartali 
            [original_title] => The Maltese Falcon
            [year] => 1941
            [link] => http://www.imdb.com/title/tt0033870/
            [rating] => 8.1
            [directors] => 
            [writers] => 
            [stars] => 
            [musicians] => 
            [languages] => 
            [countries] => 
            [time] => 100
            [imdb_id] => tt0033870
            [image] => http://localhost/works/work/sipoyler.com/public/images/filmposter/tt0033870.jpg
            [slug] => the-maltese-falcon
            [date_added] => 2016-04-05 16:11:32
            [fc_id] => 31
            [film_id] => 206
            [genres_id] => 1
            [genres_name] => Crime
        )
)

Just one genres get from this code, Crime

I want Crime and Drama together like this

Array
(
    [0] => Array
        (
            [id] => 206
            [title] => Malta Kartali 
            [original_title] => The Maltese Falcon
            [year] => 1941
            [link] => http://www.imdb.com/title/tt0033870/
            [rating] => 8.1
            [directors] => 
            [writers] => 
            [stars] => 
            [musicians] => 
            [languages] => 
            [countries] => 
            [time] => 100
            [imdb_id] => tt0033870
            [image] => http://localhost/works/work/sipoyler.com/public/images/filmposter/tt0033870.jpg
            [slug] => the-maltese-falcon
            [date_added] => 2016-04-05 16:11:32
            [fc_id] => 31
            [film_id] => 206
            [genres_id] => 1,2
            [genres_name] => Crime,Drama
        )
)

How can I solve this problem?

Thank you..

 public function getFilm($id){

      $result = $this->db->query("SELECT a.*,b.*,GROUP_CONCAT(c.genres_name ORDER BY c.genres_name ASC SEPARATOR ', ') AS genres_name ,GROUP_CONCAT(c.genres_id ORDER BY c.genres_id ASC SEPARATOR ', ') AS genres_id  FROM film_genres a
                                 LEFT JOIN film b ON a.film_id = b.id
                                 LEFT JOIN genres c ON a.genres_id = c.genres_id
                                 WHERE a.film_id=$id")->row();
      return $result;

    }

Try this

    public function getFilm($id){

      $result = $this->db->query("SELECT a.*,b.*,GROUP_CONCAT(c.genres_name ORDER BY c.genres_name ASC SEPARATOR ', ') AS genres_name ,GROUP_CONCAT(c.genres_id ORDER BY c.genres_id ASC SEPARATOR ', ') AS genres_id  FROM film_genres a
                                 LEFT JOIN film b ON a.film_id = b.id
                                 LEFT JOIN genres c ON a.genres_id = c.genres_id
                                 WHERE a.film_id=$id")->row();
      return $result;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM