php - Calling model method from controller "Call to a member function ... on a non-object", but works when called from view -


having dangdest time calling model method controller function. after many hours, , reading many, many articles on "call member function ... on non-object" , "undefined property: main::$form_model", etc., have not found solution (obviously, or not reaching out here). letting know i'm not trying take easy pass lane question.

my model folder contains model:

<?php if (!defined('basepath')) exit('no direct script access allowed');  class form_model extends ci_model {      function __construct()     {         // call 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 main class controller, loading helpers/libraries/models this:

class main extends ci_controller {       public function __construct(){         parent::__construct();         // 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. know load properly, because if change letter of model, codeigniter throws error can't load model.

the last function in main class associated form's action page. form page outer view (in "views" folder), calls inner view same name, resides in "views/content" folder, 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 "content" middle of template sandwich invokes third level view, , in case $role == 1 , "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() $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 profile update form submitted, action page "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 "views" folder, calls second-level view in "views/content" folder, called "profile_form":

<?php foreach($_post $k => $v){     echo "$k = $v <br>"; }  $this->form_model->update_profile(); ?> 

...as can see, have placed call update_profile here, , works. don't think want build website mish-mash of model functions called controller main class, while other model functions called within view pages, because that's place seem work.

in controller's main class, last function think function call should placed:

<?php if ( ! defined('basepath')) exit('no direct script access allowed');  class main extends ci_controller {       public function __construct(){         parent::__construct();         // 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 put model function call here, ci gives me "not object" error.         $this->load->view('profile_form',$data);     }  } 

update: see answer, below.

try way load model

$this->load->model('registration_model'); $this->load->model('form_model'); 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -