JOIN with two fields in the ON clause with Codeigniter (mysql+php) (MVC)

Asked

Viewed 63 times

0

I am in need of help with Codeigniter. "Fell into my lap" legacy application. Below is an attempt of mine to illustrate the problem, IE, table names and fields are "scenographic".

How to make a Join ?

I need to list the list of job names and codes depending on the company and department selected in a view,

Tabela de cargo: 
    codigo (pk), Nome

Tabela de CargosPorEmpresa: 
    id (pk), IdEmpresa(fk, idDepartamento(Fk), ramal, localizacao....
$this->db->select('cargo.Nome,cargo.Codigo', false) ;
$this->db->join('cargo',[(cargo.idEmpresa = $_POST['idEmpresa']),
                         (cargo.idDepartamento = $_POST['idDepartamento']),'');

$this->db->where('cargosPorEmpresa.idEmpresa' , $_POST['idEmpresa']);
$this->db->where('cargosPorEmpresa.idDepartamento' , $_POST['idDepartamento']);
$this->db->order_by('cargo.Nome ASC');
$this->db->group_by('cargo.Nome,cargo.Codigo');
$query = $this->db->get('cargosPorEmpresa');

1 answer

0

tries that way, can’t have a Where in a Join

return $this->db->select("cargo.Nome, cargo.Codigo", false)
        ->from(cargosPorEmpresa)
        ->join('cargo', 'cargo.idEmpresa = cargosPorEmpresa.idEmpresa')
        ->where('cargosPorEmpresa.idEmpresa' , $_POST['idEmpresa'])
        ->where('cargosPorEmpresa.idDepartamento' , $_POST['idDepartamento'])
        ->order_by('cargo.Nome ASC')
        ->group_by('cargo.Nome,cargo.Codigo')->get();;

Browser other questions tagged

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