mysql php多对多重复记录

68bkxrlz  于 2023-03-28  发布在  Mysql
关注(0)|答案(1)|浏览(94)

我有一个项目,可以有一个或多个公司的表。问题是,显示了每个选定的公司的记录,我希望所有的公司都在公司列。
It_projects模型,其中的查询(我已经删除了我没有发现必要的):

public function getTableData($sp, $userAccess, $userId, $projectId = null)
    {
        // get post data
        $requestData = $this->input->post(null, true);

        $columns = [
            0 => 'ID',
            1 => 'title',
            2 => 'company_name',
            3 => 'affected',
            4 => 'assigneeFirstname',
            5 => 'accomplished',
            6 => 'created',
            7 => 'start_date',
            8 => 'end_date',
            9 => 'estimated',
            10 => 'creatorFirstname',
            11 => 'priority',
            12 => 'statusName',
            13 => 'groupName',
        ];
       
        $select = "SELECT it_projects.ID, it_projects.title, it_projects.created, it_projects.start_date, it_projects.end_date, ";

        $select .= " firma.Nume as company_name,...";

        $from = " FROM it_projects ";
        $join .= " JOIN it_project_companies ON it_project_companies.project_id = it_projects.ID ";
        $join .= " JOIN firma ON firma.id = it_project_companies.company_id ";

It_project_companies model:

public function addCompanies($projectID, $companyIDs) {
        foreach ($companyIDs as $companyID) {
            $projectCompanyData = [
                'project_id' => $projectID,
                'company_id' => $companyID,
                'created_at' => date('Y-m-d H:i:s'),
            ];

            $this->db->insert('it_project_companies', $projectCompanyData);
        }
    }

控制器:

public function addProject()
{
    $input = $this->input->post(null, true);

    if ($this->validateFields($input)) {
        $this->load->model('projects/it_projects_notifiers');
        $this->load->model('projects/it_project_companies');
        $projectData = [
            'title' => $input['project_subject'],
            'description' => $input['project_details'],
            'creator' => $_SESSION['UserID'],
            'affected' => $input['affected_id'],
            'assignee' => $input['project_responsible'],
            'priority' => $input['project_priority'],
            'type' => $input['project_type'],
            'created' => date('Y-m-d H:i:s'),
            'company' => implode(',', $input['project_companies']),//$project_company,
            'start_date' => date('Y-m-d', strtotime($input['project_start_date'])),
            'end_date' => date('Y-m-d', strtotime($input['project_end_date'])),
            'estimated' => ($input['project_estimated_days'] * 24 + $input['project_estimated_hours']),
        ];

        if ($this->db->insert('it_projects', $projectData)) {
            $projectID = $this->db->insert_id();

            $this->it_project_companies->addCompanies($projectID, $input['project_companies']);

            $this->it_projects_notifiers->add_notifiers_affected($projectID, $input['affected_id']);

            $this->it_projects_notifiers->add_notifiers($projectID, $input['project_notifiers']);

            // add files ...
            $this->uploadFiles($_FILES, $projectID);
            $this->load->model('projects/it_projects_notifications');
            $this->it_projects_notifications->send_notifications('new_project', 0, $projectID, $_FILES);
        }
    }
}
public function projectsList()
{
    if ($this->input->is_ajax_request()) {
        $this->load->model('projects/it_projects');

        $this->output
            ->set_content_type('application/json')
            ->set_output(
                json_encode(
                    $this->it_projects->getTableData($_SESSION['SP'], $_SESSION['access'], $_SESSION['UserID'])
                )
            );
    }
}

JS公司栏目:

title: 'Company',
    targets: 2,
    type: 'html',
    data: function (source, type, val) {
        if (source.company_name) {
            return source.company_name;
        }

我发现了一些关于PHP聚合的东西,可能对我有帮助......但我不知道如何更改查询。

at0kjp5o

at0kjp5o1#

我的解决方案:

`$select .= " GROUP_CONCAT(firma.Nume SEPARATOR ', ') as company_name,..
    $group = " GROUP BY it_projects.ID ";
    $data = $this->db->query($select . $from . $join . $sql . $group . $order_limit)->result_array();`

我不再使用公共函数addCompanies,我添加了groupvar,修改了company_name,并将**$group添加到$data**中。

相关问题