簡體   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