【问题标题】:How to access the database in Helper Functions in CI4CI4中如何访问Helper Functions中的数据库
【发布时间】:2020-09-12 20:00:01
【问题描述】:

CI4:我在 Controller 中创建了数据库对象,我想执行有一些查询的辅助函数,

问题:如何在 CI4 的 Helper Functions 中访问数据库?

【问题讨论】:

  • 您不创建模型有什么原因吗?这就是它们的全部意义所在。
  • 我已经创建了模型,一些操作在整个应用程序上使用最多,所以我需要在辅助函数中执行数据库查询并返回一些结果。

标签: codeigniter codeigniter-3 codeigniter-4


【解决方案1】:

您可以通过在辅助函数中添加这些行来使用

$db = db_connect();

$result = $db->query("SELECT * FROM table")->getResult();

【讨论】:

    【解决方案2】:

    您可以从模型中访问构建器。

    $model = new SomeModel();
    $builder = $model->builder();
    $builder->db  //returns the db conn used in model
    

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2018-05-31
      • 2016-09-12
      • 2017-09-24
      • 1970-01-01
      • 2016-03-18
      相关资源
      最近更新 更多