0

ハードコードされたメソッド名ではなく、変数名でロードされたモデルのメソッドを呼び出そうとしています。これにより、大量のif-thenステートメントを使用せずに、コントローラーで大量の抽象化が可能になります。

これがモデルです

class Reports_model extends CI_Model {

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

  public function backlog()
  {
    //Do stuff
  }

私が欲しいのは、変数名でバックログ関数を呼び出せるようにすることです。コントローラは次のとおりです。

class Reports extends CI_Controller {

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

  public function get_reports($report_name)
  {
    $this->load->model('reports_model');
    $report_name = 'backlog';
    $data['data'] = $this->reports_model->$report_name();
  }

私が言えることから(そしておそらく私は愚かな何かを見逃している)、私のコードはhttp://php.net/manual/en/functions.variable-functions.phpの例2とまったく同じですが、私は得ています関数呼び出しの行でのこのエラー:

未定義のプロパティ:Reports :: $ reports_model

4

2 に答える 2

2

このモデルにはオートローダーを使用できます。オートローダーファイルはappilication/config/フォルダーにあります。モデル化する必要があります

$autoload['model'] = array('Reports_model');

またはあなたは使用することができます

class Reports extends CI_Controller {

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

  }

  public function get_reports($report_name)
  {
    $report_name = 'backlog';
    $data['data'] = $this->Reports_model->backlog();
  }
}

モデルの最初の文字の上部を次のように記述する必要があります: http: $this->Reports_model->backlog() //codeigniter.com/user_guide/general/models.html#anatomy

于 2012-07-22T14:27:49.933 に答える
1

レポートモデルをロードしていません。コントローラーでコンストラクターを変更してください。

 public function __construct() {
    parent::__construct();
    $this->load>model('Reports_model');
  }
于 2012-07-22T14:22:19.747 に答える