【问题标题】:My model doesn't work after being loaded from Controller, but works well when was loaded from another model我的模型从 Controller 加载后不起作用,但从另一个模型加载时效果很好
【发布时间】:2013-04-16 18:13:32
【问题描述】:

我是 PHP 初学者和 codeigniter 学习者。

有一个模型从Controller加载后不工作,但从另一个模型加载后工作正常。

我正在构建一个应用程序供用户获取反馈。用户可能会向听众提出几个问题。

从编码的角度来看,我有一个扩展 CI_Controller 的基本控制器“MY_Controller”。 然后我有 2 个控制器,它们扩展了我的控制器 - 主页(用户将看到的主页)和问题(查看问题的详细信息)。

我有 2 个主要型号:user_modelquestion_model

当我从 user_model 中加载 question_model 时,一切顺利,程序运行良好。

但是当我从 Question 控制器中加载 question_model 时,它会运行构造函数(我已经回显来检查它)并完成构造函数(我再次回显来检查),但是当我调用question_model 我得到了错误:

Fatal error: Call to a member function initialize() on a non-object in /Users/jaimequintas/Dropbox/3 CODIGO/feedbacking/application/controllers/question.php on line 17

有人可以帮我解决这个问题吗?我已经为此苦苦挣扎了一天多,无论如何我都无法解决。

我的基本控制器:

class MY_controller extends CI_Controller{


public function index()
{
    $this->session->set_userdata('user_id', 8); //this is here just to initialize a user while in DEV
    $this->prepare_user(); //populates user with DB info

}

我的问题控制器(不能使用 $this->question_model 方法的控制器)

class Question extends MY_Controller {

public function index(){

    parent::index();

    $active_question = $this->uri->segment(2,0);

    $this->load->model('Question_model'); //this line runs well, as an echo statement after this gets printed
    $this->Question_model->initialize($active_question); //this is the line that triggers the "can't use method error"
    $this->Question_model->get_answers_list();

这是无法从控制器调用其方法的 Question_model。

class Question_model extends CI_Model {

public $question_id;
public $question_text;
public $vote_count; 
public $activation_date;
public $status; //Draft, Active, Archived
public $question_notes; //user notes

public $question_url; //the segment that will be added to codeigniter url feedbacking.me/"semgent"
public $answers_list; //array with answer objects
public $last_vote; //date of the last vote

public $vote_count_interval; //this is not computed with initialize, must call method when needed


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

public function initialize($question_id)
{
    //populate question from DB with: question_id, question_text, vote_count, activation_date, status
    // if $question_id ==0 creates an empty question (should be followed by create_question)


        $this->question_id = $question_id;
        $this->get_question_by_id();
        $this->get_question_votes();
}

最后是 User_model。我之所以把它放在这里是因为当这个模型加载 Question_model 时,一切正常。

class User_model extends CI_Model {

public $user_id;
public $user_email;
public $user_name;
public $plan_id;
public $questions_list; //array with question objects




public function __construct()
{
    parent::__construct();
    $this->load->database();
}

public function initialize($user_id)
{
    //populates user_info and question_list
    $this->user_id = $user_id;
    $this->get_user_by_id();
    $this->get_user_questions(); //this line calls the Question_model and works fine
}

【问题讨论】:

  • 问题似乎是它试图找到 Question->QuestionModel 就像它是您的控制器的属性一样。
  • 不应该 $this->loadmodel() 在构造函数上吗?抱歉,我不太擅长 CI,因为我没有过多地使用它,但是我有一个控制器,我正在将模型加载到它的构造函数上并且工作正常。 (我也使用第二个参数来获得更短的名称)。 function __construct () { parent::__construct (); $this->load->model ( 'md_cola', 'cola' ); }
  • 嗨@Jorge,感谢您的帮助。我们可以将模型加载到控制器或另一个模型中。当我在另一个模型中加载完全相同的模型时,没关系。当我从控制器执行此操作时,它不起作用。我也使用较短的名称,但在此处将其删除以使其更简单。谢谢
  • 你能粘贴整个错误信息吗?
  • 您也缺少模型构造函数中的数据库调用。用户模型在其构造函数中有以下$this->load->database();,而您的Questions_model 没有。

标签: php codeigniter


【解决方案1】:

在模型中加载模型时,需要获取代码点火器的实例(而不是 $this):

$CI =& get_instance();
$CI->load->model('Question_model');
$CI->Question_model->initialize($active_question);
$CI->Question_model->get_answers_list();

【讨论】:

    猜你喜欢
    • 2023-01-01
    • 1970-01-01
    • 2011-06-05
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多