繁体   English   中英

从控制器调用模型方法“在非对象上调用成员函数……”,但从视图中调用时起作用

[英]Calling model method from controller “Call to a member function … on a non-object”, but works when called from view

最麻烦的是从控制器函数中调用模型方法。 经过多个小时,并且阅读了许多关于“ 在非对象调用成员函数... ”和“ 未定义的属性:Main :: $ form_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); 
    }


}

在我的主类控制器中,我正在像这样加载辅助程序/库/模型:

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

这些正确加载。 我知道它们加载正确,因为如果我更改任何模型的任何字母,CodeIgniter都会引发一个错误,表明它无法加载模型。

我的Main类中的最后一个函数与表单的操作页面相关联。 表单页面是一个外部视图(在“ views”文件夹中),它也调用一个具有相同名称的内部视图,但是位于“ views / content”文件夹中,如下所示:

<?php $this->load->view("top"); ?>
<!-- CONTENT BEGIN -->

<?php $this->load->view("content/profile_management"); ?>

<!-- CONTENT END -->
<?php $this->load->view("bottom"); ?>

您可以看到此模板三明治的“内容”中间调用了第三级视图,在$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() 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;  
}
?>

提交配置文件更新表单后,操作页面为“ profile_form”:

<?php $this->load->view("top"); ?>
<!-- CONTENT BEGIN -->

<?php $this->load->view("content/profile_form"); ?>

<!-- CONTENT END -->
<?php $this->load->view("bottom"); ?>

此页面在“视图”文件夹中,本身称为“视图/内容”文件夹中的第二级视图,称为“ profile_form”:

<?php
foreach($_POST as $k => $v){
    echo "$k = $v <br>";
}

$this->form_model->update_profile();
?>

...如您所见,我已将对update_profile的调用放在此处,并且可以正常工作。 但是我不认为我想用我的控制器Main类调用的一些模型函数来构建这个网站,而其他模型函数是在视图页面内调用的,因为这是它们似乎唯一的工作位置。

在控制器的Main类中,我认为应该放置函数调用的位置是:

<?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);
    }

}

更新:请参阅下面的答案。

尝试这种方式加载模型

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

我想我发现__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');
 }

...现在我没有收到错误,并且我的方法可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM