【问题标题】:Codeigniter - Appending variables names to a method name to run Model functionCodeigniter - 将变量名称附加到方法名称以运行模型函数
【发布时间】:2017-05-10 10:20:21
【问题描述】:

不确定是否可能,但我想做的是通过获取请求将查询字符串传递给我的控制器。从这里我使用 $_GET['name'] 获取查询字符串值。然后我想做的是使用该 GET 值并将其附加为我将传递给我的模型以返回我需要的数据的方法的名称。

这是因为多个操作将不同的查询字符串值传递给同一个控制器,然后使用该值从我的模型中的不同函数获取数据。

例如

控制器

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();
    }
}

我的模型

class My_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function customer_get_query() {
        //...run database query and return result
    }
}

有什么想法吗?

【问题讨论】:

  • 你可以试试 $this->my_model->{$queryName.'_get_query'}();
  • 非常感谢,辛苦了!

标签: php function codeigniter methods controllers


【解决方案1】:

通常的 php 应该可以正常工作:

$result = call_user_func([$this->my_model, $queryName.'_get_query']);

【讨论】:

    猜你喜欢
    • 2015-08-23
    • 1970-01-01
    • 2012-10-25
    • 2016-04-27
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 2021-03-23
    相关资源
    最近更新 更多