Codeigniter - Only one line of a LEFT JOIN

Asked

Viewed 540 times

0

I have a table that is related by the id of the other. One contains the document data, the other contains the images related to the document. More precise that in this specific query only the first row of the table of images is returned.

Follow my consultation on Codeigniter:

function getConsulta($cidade,$classe,$busca,$perpage=0,$start=0,$one=false,$array='array'){
    $this->db->select("{$classe}.*, cidades.nome as nome_cidade, categorias.nome as nome_categoria, {$classe}_imagens.imagem as imagem_imagem");
    $this->db->from($classe);
    $this->db->order_by('id','desc');
    $this->db->join('cidades', "{$classe}.cidade_id = cidades.id", 'left');
    $this->db->join('categorias', "{$classe}.categoria_id = categorias.id", 'left');
    $this->db->join("{$classe}_imagens", "{$classe}.id = {$classe}_imagens.{$classe}_id", 'left');
    $this->db->where('cidades.id', $cidade);
    $this->db->limit($perpage,$start);
    if($busca){
        $this->db->like("{$classe}.nome",$busca)->or_like("categorias.nome",$busca);
    }
    $query = $this->db->get();
    $result =  !$one  ? $query->result() : $query->row();
    return $result;
}

Today the query works normally, but only for when you have an image in the table of images. The fact that occurs when one has more than one record in the relationship and that when returning several lines the foreach interacts with each line repeating the result, but with a different image.

1 answer

1


$this->db->group_by('cidades.id'); 

You will need to group the results by city.id.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.