0

最初のモデルは 2 番目のモデルに依存しており、最初のコンストラクターで 2 番目のモデルからメソッドを呼び出す必要があります。

これまでのところ、私はこれを持っています:

ユーザー

class User extends CI_Model {

    protected $_attributes = array();

    function __construct() {
        parent::__construct();
        $this->load->model('musers');
        foreach($this->musers->GetProfile() as $key => $val) {
            $this->_attributes[$key] = $val;
        }
    }

    function __set($key, $val) {
        return $this->_attributes[$key] = $val;
    }

    function __get($key) {
        return $this->_attributes[$key];
    }
}

このモデルをautoload構成ファイルに入れたところ、次のようになりました。

A PHP Error was encountered
Severity: Notice
Message: Undefined index: load
Filename: models/user.php
Line Number: 20

Fatal error: Call to a member function model() on a non-object in  path\to\models\user.php on line 9

奇妙なこともあります-最初のエラー(通知)は linereturn $this->_attributes[$key];を参照していますが、確かに line を参照する必要があります$this->load->model('musers');

musersモデルの前にオートロードでモデルをロードしようとしましuserたが、何も役に立ちません。これを検索しようとしましたが、クエリをうまく作成できませんでした。問題の解決策があると確信しています。

私が理解しているように、これは CodeIgniter がローダー クラス自体をロードする前に 2 番目のモデルのコンストラクターが呼び出されるためですが、それはかなり奇妙です。

4

2 に答える 2

4

試す:

function __construct() {
    parent::__construct();
    $CI =& get_instance();
    $CI->load->model("musers", "musers");
    foreach($CI->musers->GetProfile() as $key => $val) {
        ......
}

    ......
于 2012-09-06T11:47:24.967 に答える
1

このようにしてみてください

$CI = &get_instance();//you need to define the instance for loading the second model musers
$CI->load->model('musers');
于 2012-09-06T11:49:04.567 に答える