【问题标题】:loading model in codeigniter在 codeigniter 中加载模型
【发布时间】:2016-08-09 04:46:25
【问题描述】:

我遇到“无法找到您指定的模型:设置”的错误 这是我的设置模型,我的文件名也是设置。它在本地主机中运行良好,但在我的服务器中上传后显示错误。我该如何解决:网络链接:http://layakdesign.co.nf/

错误显示:“无法找到您指定的模型:设置”

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Settings extends CI_Model
{
         public function __construct()
    {
        parent::__construct();
    }

    public function get_settings($code)
    {
        CI::db()->where('code', $code);
        $result = CI::db()->get('settings');

        $return = [];
        foreach($result->result() as $results)
        {
            $return[$results->setting_key]  = $results->setting;
        }
        return $return; 
    }

    /*
    settings should be an array
    array('setting_key'=>'setting')
    $code is the item that is calling it
    ex. any shipping settings have the code "shipping"
    */
    public function save_settings($code, $values)
    {
        //get the settings first, this way, we can know if we need to update or insert settings
        //we're going to create an array of keys for the requested code
        $settings   = $this->get_settings($code);

        //loop through the settings and add each one as a new row
        foreach($values as $key=>$value)
        {
            //if the key currently exists, update the setting
            if(array_key_exists($key, $settings))
            {
                $update = array('setting'=>$value);
                CI::db()->where('code', $code);
                CI::db()->where('setting_key',$key);
                CI::db()->update('settings', $update);
            }
            //if the key does not exist, add it
            else
            {
                $insert = array('code'=>$code, 'setting_key'=>$key, 'setting'=>$value);
                CI::db()->insert('settings', $insert);
            }
        }
    }

    //delete any settings having to do with this particular code
    public function delete_settings($code)
    {
        CI::db()->where('code', $code);
        CI::db()->delete('settings');
    }

    //this deletes a specific setting
    public function delete_setting($code, $setting_key)
    {
        CI::db()->where('code', $code);
        CI::db()->where('setting_key', $setting_key);
        CI::db()->delete('settings');
    }
}

【问题讨论】:

  • 我已经这样做了:“无法找到您指定的模型:设置”
  • 你没有扩展模态类
  • 更新了我的代码,但仍然是同样的错误。
  • 我的网站链接:layakdesign.co.nf
  • 你使用的是哪个codeigniter版本?

标签: codeigniter model


【解决方案1】:

您可以尝试按照以下方式创建模型。

Settings_model.php

http://layakdesign.co.nf//application/models/settings_model.php

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


class Settings_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    ...

}
?>

在特定控制器文件中加载您的模型。

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

    class Setting extends CI_Controller {

        public function __construct() {
            parent::__construct();
            $this->load->model('settings_model');
        }
}
    ?>

【讨论】:

  • 感谢您的解决方案...:)
【解决方案2】:

这是类名和文件名的问题,类名和文件名要小写

class settings extends CI_Model
{
...
}

还有文件名settings.php

【讨论】:

    【解决方案3】:

    如果你的模型文件名settings.php 而不是写像

    这样的代码
    $this->load->model('settings'); 
    

    无论你的文件名是什么,都必须在模型函数中贴花相同的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 2017-10-05
      相关资源
      最近更新 更多