Codeigniter 4 sum many columns in multiple join table throwing wrong results

39 views Asked by At

I am doing my first project using Codeigniter 4. I am trying to make a summary page which consist of some sum from different columns and different tables.

Here is the tables :

table project

id project_name
1 project 1
2 project 2

table leading | id | id_project | permit | job | | -- | ---------- |--------| ---- | 1 | 1 | 2 | 2 | | 2 | 1 | 2 | 2 | | 3 | 2 | 1 | 1 |

table man_hours | id | id_project | branch | base | sum_manhours | | -- | ---------- |--------| ---- | ------------ | | 1 | 1 | 1 | 2 | 3 | | 2 | 1 | 1 | 2 | 3 | | 3 | 2 | 1 | 1 | 2 |

I've tried this :

ProjectModel :

    public function getSummary()
    {

        $this->join('leading', 'leading.id_project=project.id', 'LEFT');
        $this->join('man_hours', 'man_hours.id_project=project.id', 'LEFT');
        $this->select('sum(permit) as permit, sum(job) as job');
        $this->select('sum(sum_manhours) as sum_manhours');
        $this->select('project.id, project.project_name');
        $this->groupBy('project.id');
        $result = $this->findAll();

        return $result;
    }

Controller Project :

public function manhours(){
        $data = [
            'title' => 'Summary',
            'project' => $this->projectModel->getSummary()
        ];

        return view('summary/view-summary-manhours', $data);
}

The results :

no project name permit leading sum_manhours
1 1 8 8 12
2 2 1 1 2

It seems like the query working twice that's why is giving the wrong results, how do I fix this? I am really looking forward for the solution, thank you so much for reading my question, even more for giving me solution.

0

There are 0 answers