簡體   English   中英

在課堂上找不到Codeigniter方法

[英]Codeigniter method not found in class

我剛開始學習CodeIgniter。 我有一些PHP背景但不是OOP。 所以我從他們的網站下載了CI並開始關注用戶指南,但我遇到了一些這樣的問題

消息:未定義的屬性:News_model :: $ load

文件名:models / news_model.php

行號:7

在那一行是__construct()函數

public function __construct()
{
    $this->load->database();
}

同樣在下一個函數db field not found in class 'News model'並且method 'result_array' not found in class...

public function get_news($slug = FALSE)
{
    if ($slug === FALSE)
    {
        $query = $this->db->get('news');
        return $query->result_array();
    }

    $query = $this->db->get_where('news', array('slug' => $slug));
    return $query->row_array();
}

我知道這是非常基本的,但我現在有點迷失了 如果有人可以解釋或者至少指出我可以學習的其他好的教程,我會很高興的。 這是完整的class News_model

class News_model extends CI_Controller {

public function __construct()
{
    $this->load->database();
}

public function get_news($slug = FALSE)
{
    if ($slug === FALSE)
    {
        $query = $this->db->get('news');
        return $query->result_array();
    }

    $query = $this->db->get_where('news', array('slug' => $slug));
    return $query->row_array();
}
}

對不起,可是:

class News_model extends CI_Controller {...}

???

是的,user2883814是正確的,CodeIgniter中的每個模型都必須只擴展CI_Model類。 所以看起來應該是這樣的:

class News_model extends CI_Model

然后,您應該將模型加載到控制器以供使用。

順便說一下,CodeIgniter中不會使用模型,而只能使用控制器和視圖。

模型應該擴展CI_Model類。

class News_model extends CI_Model { /* ... */ }

但是,使用控制器需要調用__construct的方法CI_Controller當你覆蓋類__construct方法:

class News extends CI_Controller {

    public function __construct()
    {
        // Call CI_Controller construct method first.
        parent::__construct();

        $this->load->database();
    }
}

由於您要覆蓋繼承者類中的__construct()方法,因此應首先調用父構造函數。

否則,當控制器初始化時,您將丟失LoaderCore類,並且$this->load將永遠不會工作。

暫無
暫無

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

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