亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Codeigniter 4 中的單個控制器中訪問兩個不同的模型?

如何在 Codeigniter 4 中的單個控制器中訪問兩個不同的模型?

PHP
Cats萌萌 2023-08-11 17:40:22
我是 Codeigniter 4 的新手,所以我問這個問題。事實上,我有兩個不同的型號。首先是tbl_user。其中有很多字段 tbl_user(id, name,duty_station,enlistment_date,dob) // 這里 id 是主鍵。其次是usermos。usermos 有四個字段 [id, user_id, mos_id, extra_mos_id] //這里id是主鍵,user_id是tbl_user的外鍵我已經生成了 get_profile 的控制器...我想將usermos模型訪問到get_profile控制器中。以下是我的代碼標頭<?php namespace App\Controllers;use CodeIgniter\RESTful\ResourceController;use App\Models\MosModel;use App\Models\Additional_mosModel;use App\Models\Api_auth_model;use App\Models\Api_Usermos_Model;控制器public function get_profile()    {           if (($this->request->getMethod() == 'post') && ($_SERVER['PHP_AUTH_USER'] == AUTHUSER_NAME) && ($_SERVER['PHP_AUTH_PW'] == AUTH_PASSWORD)) {        if(!empty($this->request->getPost('id'))){          $user_api_mos_model = new Api_Usermos_Model();        $usermos = $this->user_api_mos_model->findAll();           print_r($usermos);           exit;        if($profile){          if($usermos){                    }          $selected_mos = $this->getMosFromID($usermos[0]['mos_id']);                   $selected_add_mos = $this->getAdd_MosFromID($usermos[0]['additional_mos_id']);          $profile['badge'] = $this->getBadgeFromID($profile['badge_id']);          // exit;          return $this->respond([            "status" => "Success",            "message" => "Profile found.",            "Common" => ["Title" => "Load Profile API", 'version' => '1.0', 'Description' => 'Load Profile API', 'Method' => 'POST'],            "Response" => ["Userdata" => $profile,"mos" => $selected_mos,"additonal_mos" => $selected_add_mos]          ]);        }else{          return $this->respond([            "status" => "Fail",            "message" => "Profile Not found.",            "Common" => ["Title" => "Load Profile API", 'version' => '1.0', 'Description' => 'Load Profile API', 'Method' => 'POST'],            "Response" => ["Value" => 'Profile Not found.']          ]);        }      }    }  }感謝您寶貴的時間。
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

只需在控制器頁面標題部分的頂部定義模型的命名空間并在該控制器頁面中使用即可。例如:


<?php namespace App\Controllers;

 

use  app\models\User;

use app\models\Usermos;


Class UserController extend controller {

    public function actionProfile (){


      $user = new User();

      $user_mos = new Usermos();

   }

}


查看完整回答
反對 回復 2023-08-11
  • 1 回答
  • 0 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號