0

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.

4

2 回答 2

0

试试这种方式加载模型

$this->load->model('registration_model');
$this->load->model('form_model');
于 2013-10-07T12:46:33.200 回答
0

我想我发现 __construct 中对象的加载顺序很重要。这一直让我发疯。我研究了“模块助手库是否有加载顺序?” 我什么也没找到。但是,它确实有所作为。

这是我原来的父 __constructs 之一:

public function __construct(){
    parent::__construct();
    // Your own constructor code
    $this->load->helper(array('cookie','form','url'));
    $this->load->library('../controllers/accessories','form_validation');
    $this->load->model(array('login_model','registration_model','form_model'));
 }

我只是按字母顺序,“h”代表助手,“l”代表库,“m”代表模型。

我在不同模型的不同方法(对象login_model中的方法validate() )上再次遇到“不是对象”错误。这真的开始让我失望了。

我还能做什么,知道我的模型和方法在语法上是正确的,并且知道我正确地调用它们,特别是在这个问题上搜索网络并出现不足?所以,我改变了模型/库/助手的加载顺序,如下所示:

public function __construct(){
    parent::__construct();
    // Your own constructor code
    $this->load->model(array('login_model','registration_model','form_model'));
    $this->load->helper(array('cookie','form','url'));
    $this->load->library('../controllers/accessories','form_validation');
 }

...现在我没有收到错误,而且我的方法有效。

于 2013-10-07T20:38:27.150 回答