【问题标题】:codeigniter help causing error while loading modelcodeigniter 帮助在加载模型时导致错误
【发布时间】:2013-06-05 20:57:17
【问题描述】:

我不确定我的代码有什么问题......它在加载模型时导致错误...... 请帮忙........... 我的控制器

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

class Exams extends CI_Controller {
    public function index(){
        $ob = new exam_class();
        $ob->set_exam(0,'Html exam','1','20');
        $ob->create_exam();
        echo 'success';
    }
}

class exam_class{
    private $id;
    private $title;
    private $catagory;
    private $timeLength;

    function set_exam($id,$title,$catagory,$timeLength){
        $this->id = $id;
        $this->title = $title;
        $this->catagory = $catagory;
        $this->timeLength = $timeLength;
    }

    function create_exam(){
        $this->load->model('examModel');
        $this->examModel->create_exams($title,$catagory,$timeLength);
    }
}

型号

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

class ExamModel extends CI_Model {
    public function create_exams($title,$catagory,$timeLength){
        $data = array(
           'title' => $title ,
           'catagory' => $catagory ,
           'timeLength' => $timeLength
        );

        $this->db->insert('exams', $data);
    }
}

错误 遇到 PHP 错误

严重性:通知

消息:未定义的属性:exam_class::$load

文件名:controllers/exams.php

行号:26

致命错误:在第 26 行的 C:\xampp\htdocs\exam\application\controllers\exams.php 中对非对象调用成员函数 model()

【问题讨论】:

  • 为什么要在控制器中分离 2 个类?
  • 我使用单独的类来简化我的站点控制器............只是创建另一个类的对象......我是框架中的新手......可能是我的编码方法不正确....请给我任何其他建议....我发现错误我的考试类没有扩展 CI_Controller thnx 以寻求您的帮助:)
  • 尝试创建新实例来加载模型 $ci = get_instance(); $ci->load->model('模型名称');

标签: codeigniter


【解决方案1】:

您不应该在一个文件中放置多个类。控制器应该是这样的。

class Exams extends CI_Controller {

    private $id;
    private $title;
    private $catagory;
    private $timeLength;
    public function __construct()
    {
        parent::__construct();
        $this->load->model('examModel');
    }

    public function index(){
        $this->set_exam(0,'Html exam','1','20');
        $this->create_exam();
        echo 'success';
    }   

    function set_exam($id,$title,$catagory,$timeLength){
        $this->id = $id;
        $this->title = $title;
        $this->catagory = $catagory;
        $this->timeLength = $timeLength;
    }

    function create_exam(){
        $this->examModel->create_exams($title,$catagory,$timeLength);
    }
}

【讨论】:

  • 我使用单独的类来简化我的站点控制器............只是创建另一个类的对象......我是框架中的新手......可能是我的编码方法不正确....请给我任何其他建议....我发现错误我的考试类没有扩展 CI_Controller thnx 以寻求您的帮助:)
  • 嘿编码员,请按照 shin 的编码回答类型。这是使用 codeigniter 的正确方法。您使用编码器的方法更像是 C++ 方法,但在 Codeigniter 中,您最好使用其他成员函数或为此创建库和帮助程序。此外,始终在类的构造函数中加载模型。
【解决方案2】:

与@shin 一起,我想将其包含到此文件中

....\testproject\application\config\autoload.php

并编辑它以添加您的模型

$autoload['model'] = array('modelName1','modelName2');

并从任何控制器随时加载模型。这将自动加载您的模型。无需添加

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

【讨论】:

  • 我发现我的exam_class 没有扩展CI_Controller thnx 的错误是为了您的帮助:)
  • 一切正常。你可以在github上传你的测试项目然后我会修复它,thanx :)
【解决方案3】:

提示:保持简单

在您的控制器中:

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

public function index()
{
    $exam_data = $this->process_exam_data(0,'Html exam','1','20');

    $insert_status = $this->examModel->create_exams($exam_data);

    if($insert_status===TRUE){
        echo "Exam Insert Successful!";
    }
    else{
        echo "Exam Insert Failed"; 
    }
}

public function process_exam_data($id, $title, $category, $timelength)
{
   // Do whatever you want with the data, calculations etc.

   // Prepare your data array same as to be inserted into db
   $final_data = array(
                   'title'      => $processed_title,
                   'catagory'   => $processed_category,
                   'timeLength' => $processed_time
                );
   return $final_data;
}

在你的模型中:

public function create_exams($data)
{
    $result = $this->db->insert('exams', $data); // Query builder functions return true on success and false on failure
    return $result;
}

您的index 函数是执行调用的主要函数,而所有处理工作都在process_exam_data 函数中完成。

祝你有美好的一天:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2010-12-13
    • 2013-06-25
    • 2012-03-04
    • 1970-01-01
    • 2011-01-22
    • 2011-04-30
    相关资源
    最近更新 更多