在CodeIgniter for Oracle中设置数据库模式

问题描述 投票:2回答:1

我正在尝试学习CodeIgniter,并且只是弄脏了MVC架构的model组件。

我首先尝试使用查询构建器,但发现它不断向我的表/列名称添加引号。解决这个问题的方法是使用它们上的所有大写并修复它。我讨厌全部大写,所以我决定不使用查询构建器而是做$this->db->query();

我现在遇到的问题是我必须不断用表名编写模式:SELECT * FROM schema.table_name。我想避免为我需要引用的所有表编写schema.

我浏览了文档并看到了a configuration for setting schemas,但它似乎只用于PostgreSQL和ODBC驱动程序。我仍然尝试过,但当然它没有像我预期的那样工作。我仍然得到一个错误,说找不到表。

笔记:

我以我的用户身份登录但我需要以不同的用户身份运行查询 - 即以user1身份登录但需要以user2.table_name身份运行查询。

php oracle codeigniter schema
1个回答
1
投票

所以我能够通过在我的模型类上添加__construct()来解决这个问题。

class My_model extends CI_Model {
  public function __construct() {
    $this->db->query("ALTER SESSION SET CURRENT_SCHEMA = my_schema");
  }

  public function get_awesome_user() {
    $this->db->select("u.username, u.firstname, u.lastname");
    $this->db->from("sys_users u");
    $this->db->where("u.username", $this->input->post("username"));

    $query = $this->db->get();
    return $query->result();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.