简体   繁体   English

在课堂上找不到Codeigniter方法

[英]Codeigniter method not found in class

I just started to learn CodeIgniter. 我刚开始学习CodeIgniter。 I have some background in PHP but not in OOP. 我有一些PHP背景但不是OOP。 So I've downloaded the CI from their website and started to follow the user guide but I faced some problems like this 所以我从他们的网站下载了CI并开始关注用户指南,但我遇到了一些这样的问题

Message: Undefined property: News_model::$load 消息:未定义的属性:News_model :: $ load

Filename: models/news_model.php 文件名:models / news_model.php

Line Number: 7 行号:7

On that line is the __construct() function 在那一行是__construct()函数

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

Also in the next function db field not found in class 'News model' and method 'result_array' not found in class... 同样在下一个函数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();
}

I know that this is very basic but I'm a bit lost right now . 我知道这是非常基本的,但我现在有点迷失了 I'll be glad if someone can explain or at least point me to another good tutorial/s that I can learn. 如果有人可以解释或者至少指出我可以学习的其他好的教程,我会很高兴的。 Here is the full class News_model 这是完整的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();
}
}

sorry but: 对不起,可是:

class News_model extends CI_Controller {...}

??? ???

Yes, user2883814 is right, every model in CodeIgniter must extend only CI_Model class. 是的,user2883814是正确的,CodeIgniter中的每个模型都必须只扩展CI_Model类。 So it should look like this: 所以看起来应该是这样的:

class News_model extends CI_Model

Then you should load your model to controller for using. 然后,您应该将模型加载到控制器以供使用。

By the way models aren't used in CodeIgniter so often, and you can use only controllers and views instead. 顺便说一下,CodeIgniter中不会使用模型,而只能使用控制器和视图。

Models should extend the CI_Model class. 模型应该扩展CI_Model类。

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

However, using Controllers you need to call __construct method of CI_Controller class when you override the __construct method: 但是,使用控制器需要调用__construct的方法CI_Controller当你覆盖类__construct方法:

class News extends CI_Controller {

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

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

Since you're overriding the __construct() method within the inheritor class, you should call the parent constructor at first. 由于您要覆盖继承者类中的__construct()方法,因此应首先调用父构造函数。

Otherwise, when the controller is initializing, you'll lose Loader and Core class and $this->load will never work. 否则,当控制器初始化时,您将丢失LoaderCore类,并且$this->load将永远不会工作。

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

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