Having the dangdest time calling a model method from a controller function. After many hours, and reading many, many articles on "Call to a member function ... on a non-object" and "Undefined property: Main::$form_model", etc., I have not found any solution (obviously, or I would not be reaching out here). Just letting you know I'm not trying to take the Easy Pass lane with this question.
My model folder contains this model:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Form_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function update_profile(){
$data = array();
$formValues = $this->input->post();
$this->db->where('member_id', $data['member_id']);
$this->db->update('members', $data);
}
}
In my Main class controller, I am loading helpers/libraries/models like this:
class Main extends CI_Controller {
public function __construct(){
parent::__construct();
// Your own constructor code
$this->load->helper(array('cookie','form','url'));
$this->load->library('../controllers/accessories');
$this->load->model(array('registration_model','form_model'));
}
These load properly. I know they load properly, because if I change any letter of any model, CodeIgniter throws up an error that it can't load the model.
The last function in my Main class is associated with a form's action page. The form page is an outer view (in the "views" folder), which also calls an inner view with the same name, but which resides in a "views/content" folder, like so:
<?php $this->load->view("top"); ?>
<!-- CONTENT BEGIN -->
<?php $this->load->view("content/profile_management"); ?>
<!-- CONTENT END -->
<?php $this->load->view("bottom"); ?>
You can see the "content" middle of this template sandwich invokes a third level view, and in the case where $role == 1
, that would be "forms/profile_manager_form":
<?php
if ($this->uri->segment(3) === FALSE){
if($this->input->cookie('member')){
$member_id = $this->input->cookie('member');
}
else{
$member_id = 0;
}
}
else{
$member_id = $this->uri->segment(3);
}
$query = $this->db->get_where('members', array('member_id' => $member_id));
foreach ($query->result() as $row){
$role = $row->role;
}
switch($role){
case "1" :
$this->load->view('forms/profile_manager_form');
break;
case "2" :
$this->load->view('forms/portfolio_manager_analyst');
break;
}
?>
When the profile update form is submitted, the action page is "profile_form":
<?php $this->load->view("top"); ?>
<!-- CONTENT BEGIN -->
<?php $this->load->view("content/profile_form"); ?>
<!-- CONTENT END -->
<?php $this->load->view("bottom"); ?>
This page, in the "views" folder, itself calls a second-level view in the "views/content" folder, called "profile_form":
<?php
foreach($_POST as $k => $v){
echo "$k = $v <br>";
}
$this->form_model->update_profile();
?>
...as you can see, I have placed the call to update_profile
here, and it works. But I don't think I want to build this website with a mish-mash of some model functions called from my controller Main class, while other model functions are called from within view pages, because that's the only place they seem to work.
In my controller's Main class, my last function is where I think the function call should be placed:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
public function __construct(){
parent::__construct();
// Your own constructor code
$this->load->helper(array('cookie','form','url'));
$this->load->library('../controllers/accessories');
$this->load->model(array('registration_model','form_model'));
}
public function index()
{
$config = array();
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
$data = array();
$data['scripts'] = $this->accessories->getScripts($config);
$this->load->view('home',$data);
}
public function profile_form(){
$config = array();
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
$data = array();
$data['states'] = $this->accessories->states();
$data['scripts'] = $this->accessories->getScripts($config);
$this->form_model->update_profile(); //I want to put my model function call here, but CI gives me the "not an object" error.
$this->load->view('profile_form',$data);
}
}
UPDATE: See my answer, below.