2013-10-07 12 views
0

稱爲具有dangdest時間調用從控制器功能的模型方法時。許多小時,並在「調用一個成員函數...非對象開」和「未定義的屬性:主要:: $ form_model」讀很多很多的文章等處理後,我還沒有發現任何解決方案(顯然,或者我不會在這裏伸出)。只是讓你知道我並沒有試圖將這個問題帶到Easy Pass車道。調用模型方法「調用一個成員函數...無對象」,但工程從視圖

我的模型文件夾中包含該型號:

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

這些負載正常。我知道他們正確加載,因爲如果我改變任何模式中的任何字母,笨拋出了它不能加載模型中的錯誤。

在我的主類中的最後一個功能與形式的操作頁面相關聯。表單頁面是外視圖(在「意見」文件夾中),這也要求具有相同名稱的內視圖,但它駐留在一個「視圖/內容」文件夾中,像這樣:

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

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

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

您可以看到這個模板三明治的「內容」中調用第三級視圖,並在情況下$role == 1,那將是「表格/ 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類調用一些模型函數的混搭來構建這個網站,而其他模型函數是從視圖頁面中調用的,因爲這是他們似乎工作的唯一地方。

在我的控制器的主類,我的最後一個功能是我覺得函數調用應放在:

<?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: 見我的答案,以下。

+0

哇,有些故事...也許嘗試縮短你的答案,只顯示必要的部分,沒有它我們將無法幫助,很多人不會讀這麼長的問題 –

+1

我給了適量的信息那個「特殊」的人實際上可以幫忙;) – TARKUS

回答

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)。這真的開始打敗我了。

我還能做什麼,知道我的模型和方法在語法上是正確的,並且知道我正確地調用了它們,特別是在網上搜索這個問題並縮短了這個問題?所以,我拖着模型/庫/傭工的加載順序,就像這樣:

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

...現在我不明白的錯誤,我的方法的工作。

0

試試這個方式來加載模型

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

實際上,你可以使用數組加載模型(至少你可以在最後一個版本之前,如果它仍然是相同的,我沒有檢查它) –

+0

是的,我試過了這兩種方式對我遇到的錯誤都沒有影響。 – TARKUS

相關問題