我是一个新的初学者在Codeigniter 4,所以我要问这个问题。
实际上,我有两种不同的型号。
首先是tbl_user.
在许多字段中,tbl_user(id、name、duty_station、enlistment_date、道布) //这里id是主键。
第二个是usermos.
usermos有四个字段id,user_id、mos_id、additional_mos_id //hereid是主键,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.']
]);
}
}
}
}谢谢你宝贵的时间。
发布于 2020-07-18 13:32:11
只需在控制器页标题部分的顶部定义模型的命名空间,并在该控制器页中使用。例如:
<?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();
}
}https://stackoverflow.com/questions/62968932
复制相似问题