Codeigniter - 将变量名称附加到 运行 模型函数的方法名称
Codeigniter - Appending variables names to a method name to run Model function
不确定是否可能,但我想做的是通过 get 请求将查询字符串传递到我的控制器。从这里我使用 $_GET['name'] 获取查询字符串值。然后我想做的是使用该 GET 值并将其作为我将传递给我的模型的方法的名称附加到 return 我需要的数据。
这是因为多个操作将不同的查询字符串值传递给同一个控制器,然后使用该值从我的模型中的不同函数获取数据。
例如
控制器
class ControllerName extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('my_model');
}
public function index() {
$queryName = $_GET['query_string']
// e.g. $queryName = 'customer'
//Use the query string and append to method to pass to Model function
$result = $this->my_model->$queryName._get_query(); //???
//e.g. $result = $this->my_model->customer_get_query();
}
}
My_model
class My_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function customer_get_query() {
//...run database query and return result
}
}
有什么想法吗?
通常的 php 应该可以正常工作:
$result = call_user_func([$this->my_model, $queryName.'_get_query']);
不确定是否可能,但我想做的是通过 get 请求将查询字符串传递到我的控制器。从这里我使用 $_GET['name'] 获取查询字符串值。然后我想做的是使用该 GET 值并将其作为我将传递给我的模型的方法的名称附加到 return 我需要的数据。
这是因为多个操作将不同的查询字符串值传递给同一个控制器,然后使用该值从我的模型中的不同函数获取数据。
例如
控制器
class ControllerName extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('my_model');
}
public function index() {
$queryName = $_GET['query_string']
// e.g. $queryName = 'customer'
//Use the query string and append to method to pass to Model function
$result = $this->my_model->$queryName._get_query(); //???
//e.g. $result = $this->my_model->customer_get_query();
}
}
My_model
class My_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function customer_get_query() {
//...run database query and return result
}
}
有什么想法吗?
通常的 php 应该可以正常工作:
$result = call_user_func([$this->my_model, $queryName.'_get_query']);