コントローラー関数からモデルメソッドを呼び出すのが最も危険です。何時間もかけて、「メンバー関数への呼び出し ... 非オブジェクトで」や「未定義のプロパティ: Main::$form_model 」などに関する記事をたくさん読んだ後、解決策が見つかりませんでした (明らかに、さもなければ、私はここに手を差し伸べることはありません)。この質問でイージー パス レーンを使用しようとしているわけではないことをお知らせします。
私のモデルフォルダーには、このモデルが含まれています:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Form_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function update_profile(){
$data = array();
$formValues = $this->input->post();
$this->db->where('member_id', $data['member_id']);
$this->db->update('members', $data);
}
}
メイン クラス コントローラでは、次のようにヘルパー/ライブラリ/モデルをロードしています。
class Main extends CI_Controller {
public function __construct(){
parent::__construct();
// Your own constructor code
$this->load->helper(array('cookie','form','url'));
$this->load->library('../controllers/accessories');
$this->load->model(array('registration_model','form_model'));
}
これらは正しくロードされます。モデルの文字を変更すると、CodeIgniter はモデルをロードできないというエラーをスローするため、それらが適切にロードされることはわかっています。
Main クラスの最後の関数は、フォームのアクション ページに関連付けられています。フォーム ページは外部ビュー ("views" フォルダー内) であり、同じ名前の内部ビューも呼び出しますが、次のように "views/content" フォルダー内にあります。
<?php $this->load->view("top"); ?>
<!-- CONTENT BEGIN -->
<?php $this->load->view("content/profile_management"); ?>
<!-- CONTENT END -->
<?php $this->load->view("bottom"); ?>
このテンプレート サンドイッチの「コンテンツ」中央が第 3 レベルのビューを呼び出すことがわかります。この場合$role == 1
、それは「forms/profile_manager_form」になります。
<?php
if ($this->uri->segment(3) === FALSE){
if($this->input->cookie('member')){
$member_id = $this->input->cookie('member');
}
else{
$member_id = 0;
}
}
else{
$member_id = $this->uri->segment(3);
}
$query = $this->db->get_where('members', array('member_id' => $member_id));
foreach ($query->result() as $row){
$role = $row->role;
}
switch($role){
case "1" :
$this->load->view('forms/profile_manager_form');
break;
case "2" :
$this->load->view('forms/portfolio_manager_analyst');
break;
}
?>
プロファイル更新フォームが送信されると、アクション ページは "profile_form" になります。
<?php $this->load->view("top"); ?>
<!-- CONTENT BEGIN -->
<?php $this->load->view("content/profile_form"); ?>
<!-- CONTENT END -->
<?php $this->load->view("bottom"); ?>
「views」フォルダー内のこのページ自体が、「views/content」フォルダー内の「profile_form」という第 2 レベルのビューを呼び出します。
<?php
foreach($_POST as $k => $v){
echo "$k = $v <br>";
}
$this->form_model->update_profile();
?>
...ご覧のとおり、update_profile
ここに電話をかけましたが、機能しています。しかし、コントローラーの Main クラスから呼び出されるいくつかのモデル関数をごちゃまぜにしてこの Web サイトを構築したいとは思いませんが、他のモデル関数はビュー ページ内から呼び出されます。
私のコントローラーの Main クラスでは、最後の関数は、関数呼び出しを配置する必要があると思われる場所です。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
public function __construct(){
parent::__construct();
// Your own constructor code
$this->load->helper(array('cookie','form','url'));
$this->load->library('../controllers/accessories');
$this->load->model(array('registration_model','form_model'));
}
public function index()
{
$config = array();
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
$data = array();
$data['scripts'] = $this->accessories->getScripts($config);
$this->load->view('home',$data);
}
public function profile_form(){
$config = array();
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
$data = array();
$data['states'] = $this->accessories->states();
$data['scripts'] = $this->accessories->getScripts($config);
$this->form_model->update_profile(); //I want to put my model function call here, but CI gives me the "not an object" error.
$this->load->view('profile_form',$data);
}
}
更新: 以下の私の回答を参照してください。