0

I'm very new to CodeIgniter and not an expert in OOP so please bear with me.

This is the function I have in my model:

function get_company(int $user_id, $fields = '*'){
    $r = $this->db->query("SELECT $fields FROM ".$this->db->dbprefix('companies')." WHERE user_id=?", $user_id)->row();
    return $r;        
}
function get_profile($user_id, $fields = '*'){
    $r = $this->db->query("SELECT $fields FROM ".$this->db->dbprefix('users_profiles')." WHERE user_id=?", $user_id)->row();
    return $r;        
}

This is in my controller that is calling that model:

function index(){ 
    $this->load->model('profiles_m');
    $profile = $this->profiles_m->get_profile($this->access->getUid());
    $company = $this->profile_m->get_company($this->access->getUid());      

    $vars = array(
            'profile'=>$profile, 
            'company'=>$company,        
        );

    $this->_getTemplate()->build('account', $vars);
}

An in my view:

$company = array(
        'name'     => 'company',
        'id'          => 'company',
        'value'       => "$company->name",
        'class'       => 'styl_f validate[required] text-input input-xlarge',
        'placeholder' => "$company->name"
);

echo $company['value']

The error I am getting is this: Call to a member function get_company() on a non-object in C:\..\application\modules\accounts\controllers\accounts.php I am under the impression that I am receiving these errors because I am passing a non object through get_company() but the thing that confuses me is that this error does not come up for get_profile(); The get_profile() function in my model is very similiar to my get_company() function. What is causing this error? How can I get rid of it?

tereško
  • 58,060
  • 25
  • 98
  • 150
Zaki Aziz
  • 3,592
  • 11
  • 43
  • 61

3 Answers3

2

The problem is within your controller:

function index(){ 
    $this->load->model('profiles_m');
    $profile = $this->profiles_m->get_profile($this->access->getUid());
    $company = $this->profile_m->get_company($this->access->getUid()); // Right here

    $vars = array(
            'profile'=>$profile, 
            'company'=>$company,        
        );

    $this->_getTemplate()->build('account', $vars);
}

The $profile variable uses $this->profiles_m as the object, but $company misses the letter 's' in the object.

Try with this line instead:

    $company = $this->profiles_m->get_company($this->access->getUid());
Repox
  • 15,015
  • 8
  • 54
  • 79
1

You have a typo, the line should read:

$company = $this->profiles_m->get_company($this->access->getUid()); 

Notice 'profiles_m' and not 'profile_m'.

Zvonko Biskup
  • 393
  • 2
  • 9
1
 $company = $this->profile_m->get_company($this->access->getUid());

replace 'profile_m' to 'profiles_m'

voodoo417
  • 11,861
  • 3
  • 36
  • 40