簡體   English   中英

使用CodeIgniter db和活動記錄時出現“未定義的屬性”錯誤

[英]“Undefined property” error when using CodeIgniter db and active record

我正在嘗試使用活動記錄和codeigniter從一些簡單的形式插入演示記錄

function create_httpPost()
{
   $data = array(
      'title' => $this->input->post('title'),
      'content' => $this->input->post('content')
   );

   $this->newsModel->createData($data); //error occures here
   $this->index();//aka redirectToAction
}

但發布表格后,我得到以下錯誤

**消息:未定義的屬性:新聞:: $ newsModel

文件名:controllers / news.php行號:29 **

內部模型我有這種方法

function createData($data)
    {
        $this->db->insert('News', $data);
        return;
    }

我在這里做錯了什么?

根據CodeIgniter文檔,模型類名稱必須以大寫字母開頭,其余名稱為小寫字母。

請參閱: http : //ellislab.com/codeigniter/user-guide/general/models.html

在標題為“模型解剖”的部分中

類名的首字母必須大寫,其余名稱小寫...文件名將是類名的小寫版本。

在您的情況下, newsModel違反了規則,並且CodeIgniter名稱解析器可能找不到該類(或相關的.php文件),這就是為什么它認為newsModel是一個屬性(不存在)。

嘗試這個:

function create_httpPost()
{
   $this->load->model('newsModel'); // you need to load model

   $this->newsModel->createData(array(
      'title'   => $this->input->post('title', TRUE),
      'content' => $this->input->post('content', TRUE)
   ));

   $this->index();
}

這是CI2非常常見的問題。

此功能必須在您的型號中嗎?

如果是這樣,請注意, u can't reference model from inside other model 由於某些原因。 你需要讓控制器來處理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM