简体   繁体   中英

I can't do inner join in Codeigniter and combine with where condition

I want to join two tables projects and events_projects, i have column relation in table events_projects which have number of project id. I want ot get events which relate to this project. In view i have one project and timeline with events which relate ti this project. I tried to do this inner join, but i get just one event of this project, but in database i have few events of this project. How can i get all events of project?

This if my function in Model:

 public function getProject($id){
        $this->db->select('projects.*, events_projects.*');
        $this->db->from('projects');
        $this->db->where('projects.project_id', $id);
        $this->db->join('events_projects', 'projects.project_id = events_projects.relation', 'inner');
        $query = $this->db->get();
        $project = $query->result_array()[0];
        return $project;
    }

Try to change $this->db->from('projects') with $this->db->from('events_projects') :

public function getProject($id){
    $this->db->select('projects.*, events_projects.*');
    $this->db->from('events_projects');
    $this->db->join('projects', 'projects.project_id = events_projects.relation', 'inner');
    $this->db->where('projects.project_id', $id);
    $query = $this->db->get();
    $project = $query->result_array()[0];
    return $project;
}

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