Codeigniter 控制器的构造函数中加载的模型不可被同一控制器的其他函数使用

发布于 2024-12-19 11:27:16 字数 1432 浏览 0 评论 0原文

我的模型:如下所示,非常基本

class User extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function getAll()
    {
        $this->db->order_by("lastName", "asc");
        $this->db->order_by("firstName", "asc");
        $this->db->order_by("userName", "asc");
        $query = $this->db->get('user');

        // test for result
        if($query->num_rows() > 0)
        {
            return $query->result();
        }

        return NULL;
    }
}

我的控制器:实际上是我控制器的一部分,每次通过默认路由加载用户/显示功能时,都会显示错误(进一步向下)。控制器构造函数中加载的模型是否可用于同一控制器中的所有其他功能?

class Users extends CI_Controller
{
    function __contruct()
    {
        parent::__construct();
        $this->load->model('user');
    }

    function display()
    {
        $data['users'] = $this->user->getAll();

        $head['pageTitle'] = 'Users Panel';

        $this->load->view('security/redirect');
        $this->load->view('template/head', $head);
        $this->load->view('user/usersPanel', $data);
        $this->load->view('template/foot');
    }
}

我的错误:在上面的控制器中引用行“$data['users'] = $this->user->getAll()”

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$user

我的环境:

Codeigniter 2.1.0;
Mac Lion;
MAMP 2.0;

My model: as seen below, very basic

class User extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function getAll()
    {
        $this->db->order_by("lastName", "asc");
        $this->db->order_by("firstName", "asc");
        $this->db->order_by("userName", "asc");
        $query = $this->db->get('user');

        // test for result
        if($query->num_rows() > 0)
        {
            return $query->result();
        }

        return NULL;
    }
}

My controller: actually part of my controller, every time loading the users/display function by default route, the error (further down) shows up. Should a model loaded in a controller's contructor be available for all other function in the same controller?

class Users extends CI_Controller
{
    function __contruct()
    {
        parent::__construct();
        $this->load->model('user');
    }

    function display()
    {
        $data['users'] = $this->user->getAll();

        $head['pageTitle'] = 'Users Panel';

        $this->load->view('security/redirect');
        $this->load->view('template/head', $head);
        $this->load->view('user/usersPanel', $data);
        $this->load->view('template/foot');
    }
}

My error: refering to the line, "$data['users'] = $this->user->getAll()", in above controller

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Users::$user

My environment:

Codeigniter 2.1.0;
Mac Lion;
MAMP 2.0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ映画 2024-12-26 11:27:16

难道不应该这样:

class Users extends CI_Controller
{
    function __contruct()
    {

像这样:

class Users extends CI_Controller
{
    function __construct()
    {

用构造替换构造。

Shouldn't this:

class Users extends CI_Controller
{
    function __contruct()
    {

be like this:

class Users extends CI_Controller
{
    function __construct()
    {

replace contruct with construct.

瑕疵 2024-12-26 11:27:16

难道不应该是这样

$data['users'] = $this->user->getAll();

$data['users'] = $this->user_model->getAll();

抱歉
还有模型名称:

$this->load->model('user_model');

类名 User_model 扩展了 CI_Model

我所有的 CI 项目都是这样设置的。

http://codeigniter.com/user_guide/general/models.html

Shouldn't this:

$data['users'] = $this->user->getAll();

be this:

$data['users'] = $this->user_model->getAll();

sorry
also the model name:

$this->load->model('user_model');

and class name User_model extends CI_Model

All of my CI projects are set up this way.

http://codeigniter.com/user_guide/general/models.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文