【问题标题】:Load a model in Codeigniter 3.0.3在 Codeigniter 3.0.3 中加载模型
【发布时间】:2016-12-22 16:35:41
【问题描述】:

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

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

在我的模型文件夹中:

  1. Place.php(里面的名字是class Place extends ActiveRecord\Model)
  2. 用户.php
  3. ...

如果我想加载一个模型,我必须在我的控制器 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

【问题讨论】:

  • 试试$this-&gt;Place-&gt;get_all_places()
  • 我试过但没有用。好像不加载模型。我得到的错误信息是:无法找到您指定的模型:place
  • 添加更多的控制器和模型代码,这样很难找到问题。
  • 尝试从“ActiveRecord\Model”类中的“CI_Model”扩展...如果您需要使用另一种方式加载模型(自定义模型),则需要从 CI_Model 扩展:T 然后,创建它的库而不使用 CI 中的模型系统

标签: php codeigniter codeigniter-classloader


【解决方案1】:

你的模型文件名应该是

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

【讨论】:

  • 但我必须更改必要的 CI_Model?因为我的整个项目都使用这个类 Place extends ActiveRecord\Model =S
【解决方案2】:

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

class Place extends ActiveRecord\Model

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

class Place extends ActiveRecordModel

如果您还没有定义任何新模型,则可以另一种解决方案。然后将上面的行替换为

 class Place extends CI_Model

【讨论】:

  • 嗨@Hikmat Sijapat 我试试你说的但不起作用。总是相同的错误无法找到您指定的模型:place、Place 或 Place_Model
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 2017-10-05
相关资源
最近更新 更多