dongshushi5579 2018-08-30 20:51
浏览 149
已采纳

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

I'm trying to learn CodeIgniter and just getting my hands dirty with the model component of the MVC architecture.

I first tried using the query builder but found that it keeps adding quotes to my table/column names. A way to fix that is to use all caps on them and that fixed it. I hate writing in all caps though so I decided to just not use the query builder and do $this->db->query(); instead.

The issue I have with this right now is that I have to constantly write the schema with the table name: SELECT * FROM schema.table_name. I would like to avoid having to write schema. for all tables I need to reference.

I browsed the documentation and saw a configuration for setting schemas however it seems like it's only used with PostgreSQL and ODBC drivers. I still tried it but of course it didn't work as I expected. I still get an error saying the table could not be found.

NOTES:

I am logged in as my user but I need to run queries as a different user - i.e. logged in as user1 but need to run queries as user2.table_name.

  • 写回答

1条回答 默认 最新

  • donglang8008 2018-08-31 18:03
    关注

    So I was able to solve this by adding a __construct() on my model class.

    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();
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,
  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。