skip to Main Content

The error: (applicationmodels/projects/It_project_companies.php exists, but doesn’t declare class It_project_companies), occurs when I try saving a project. I am loading the model in the save method from controller:

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'); //my model

        $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' => $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);
        }
    }
}

and addCompanies method from model:

    class It_project_companies extends My_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function addCompanies($projectID, $companyIDs) {
        foreach ($companyIDs as $companyID) {
            $projectCompanyData = [
                'project_id' => $projectID,
                'company_id' => $companyID,
            ];
            $this->db->insert('it_project_companies', $projectCompanyData);
        }
    }
}

I must add that currently I didn’t made the queries to join the needed tables etc…because I am not sure how to make them and the error still occurred when I had the queries, probably written wrong…

2

Answers


  1. Chosen as BEST ANSWER

    The solution for me was to remove the namespace, because I don't have autoload in composer.json. I don't know if it's a good approach to load the model with $this->load->model(), or I should Autoload Classes With Composer.


  2. ## Your model should look like this ##
    
    <?php
    defined('BASEPATH') or exit('No direct script access allowed');
    
    class It_project_companies extends CI_Model
    {
        public function addCompanies($projectID, $companyIDs){}
    }
    
    ## but since you are extending ##
    <?php
        defined('BASEPATH') or exit('No direct script access allowed');
    
        class It_project_companies extends My_Model
        {
            public function addCompanies($projectID, $companyIDs){}
        }
    

    I don’t know why you had a ‘/’ before My_Model

    ## My_Model ##
    <?php
    defined('BASEPATH') or exit('No direct script access allowed');
    
    class MY_Model extends CI_Model
    {
     public function __construct(){}
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search