私は最近、OOPとCodeigniterを学び始めました。私はコアに2つの新しいファイルを設定しました。 CI_Controllerを拡張するMY_Controller.phpとCI_Modelを拡張するMY_Model.php。これらのファイルは両方とも動作しています。さまざまなコントローラとモデルでメソッドを呼び出すことができます。しかし、MY_Controllerには、ユーザーがログインしているかどうかをチェックするメソッドがあります。その場合は、ユーザーテーブルの最後のアクティブなフィールドを更新するMY_Modelからメソッドを呼び出します。私が言うLogin_modelからそれを呼び出すときに、この方法が働いているが、私はMY_Controllerからそれを呼び出すときにエラーを渡しますコードネーター:コントローラークラスからモデルメソッドを呼び出す方法は?
Call to undefined method Feed::update_last_active()
これはなぜですか?コアコントローラからコアモデルを呼び出そうとしていますが、これをやってはいけませんか?以下は私のコードです。
MY_Controller.php:
class MY_Controller extends CI_Controller{
public function __construct(){
parent::__construct();
}
/**
* Check if the users sessions logged in
*/
public function logged_in(){
//Check the flag logged_in exists in the session
if ($this->session->userdata('logged_in')){
//Update the last active field in the user db
$this->update_last_active($this->session->userdata('user_id'));
return true;
} else {
return false;
}
}
}
MY_Model.php:@Tiger応答に更新
class MY_Model extends CI_Model{
/**
* Updates users last active
*/
public function update_last_active($id){
$this->db->where('id', $id);
$this->db->update('users', array('last_active' => date('Y-m-d H:i:s')));
}
}
MY_Controller(Call to undefined method CI_Loader::update_last_active()
を返します):
public function logged_in(){
//Check the flag logged_in exists in the session
if ($this->session->userdata('logged_in')){
//Load my model
$my_model = $this->load->model('MY_Model');
//Update the last active field in the user db
$my_model->update_last_active($this->session->userdata('user_id'));
return true;
} else {
return false;
}
}
あなたはupdate_last_activeメソッドを使用するようにMY_Modelインスタンスを作成する必要があります。コントローラーにはその名前のメソッドがありません。 –