在Codeigniter 3.0.3中加载模型

nbysray5  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(145)

我在控制器文件夹中有一个具有此结构的项目:

  • places.php
  • users.php
  • items.php
  • ...

在我的模型文件夹中:

  1. Place.php(里面的名称是类Place扩展ActiveRecord\Model)
  2. User.php
    1.一个人。
    在我的控制器places.php中,如果我想加载一个模型,我必须这样做:
$this->load->model('Place');

然后我必须这样调用我的方法:

$this->place->get_all_places();

它是在我的本地主机工作,但不是在我的服务器,我检查我的php版本在服务器,它的5.6.
我该怎么补救?
这是我的模型文件Place.php

class Place extends ActiveRecord\Model
    {
        static $table_name = 'places';
        static $has_many = array(
        );

        public function get_all_places()
        { 
            return true;
        }
}

这是我的控制器文件places.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Places extends MY_Controller {

        function __construct()
        {
            parent::__construct();

            if($this->user){
                $access = TRUE;

            }else{
                redirect('login');
            }

        }   
        function index()
        {
            $this->content_view = 'places/all';
        }

        function get_places(){

            $this->theme_view = '';

            $this->load->model('Place');
            $this->place->get_all_places();
        }
}

错误在于:

Unable to locate the model you have specified: place
jqjz2hbq

jqjz2hbq1#

模型文件名应为

Places_model.php

和内部模型

class Places_Model extends CI_Model # Changed 
{
    static $table_name = 'places';
    static $has_many = array();

    public function get_all_places()
    { 
        return true;
    }

}

Models in Codeigniter

pvcm50d1

pvcm50d12#

我认为每件事都是对的。但问题是由于这条线。

class Place extends ActiveRecord\Model

如果您定义了一个扩展了CI_Model的新模型。然后新模型的名称不能像ActiveRecord\Model。名称可能是ActiveRecordModel。因此,您的一个解决方案可以是..如果您在application/core文件夹中定义了名为ActiveRecordModel的新模型,请将上面的行替换为下面的行。

class Place extends ActiveRecordModel

另一个解决方案是,如果你还没有定义任何新的模型,那么就把上面的行替换为

class Place extends CI_Model

相关问题