Search code examples
phporacle-databasecodeignitercodeigniter-3oracle12c

CodeIgniter e Oracle - Double quotes in table name - ActiveRecords


I am having the following problem (table or view not exists). I'm using CodeIgniter 3 and Oracle 12c.

enter image description here

My model:

public function getAll(){
      $query = $this->db->get('usr');
      return $query->result_array();
}

I've tried a lot and no solution has solved the problem for my case. For everyone referred to the codeIgniter 2 or older version.

If I use the following query, the code works. But I want to use ActiveRecords:

SELECT * FROM usr

This is the same problem CodeIgniter and Oracle database - ActiveRecord insert() is adding double quotes in query. But the solution doesn't work for me.


Solution

  • I find the solution: https://forum.codeigniter.com/archive/index.php?thread-47389.html

    Solutions in portuguese: https://wordivino.blogspot.com/2019/01/codeigniter-e-oracle-12c-message.html

    The last post:


    Please, look at the SQL query generated by CI:

    SELECT * FROM "project_users" WHERE "userid" = 'user1' AND "password" = 'iamuser1'
    

    Note that the table names and table fields has double quotes, and that the reason of the issue: in a oracle SQL query, you DONT escape the name of the identifiers.

    Looking in the source of CI, de base class CI_DB has a flag indicating that identifiers MUST be escaped. This is not necessary in Oracle, so i just add this attribute in system/database/drivers/oci8_diver.php...

    var $_protect_identifiers = FALSE;
    

    (If someone else want to test it, I put the line above before the method db_connect() on the OCI 8 Driver).


    In my case, I put the variable after class CI_DB_oci8_driver extends CI_DB {

    class CI_DB_oci8_driver extends CI_DB {
    
        var $_protect_identifiers = FALSE;
        [...]
    

    Another way to solve this problem is by modifying DB_driver.php (system/database/DB_driver.php).

    Find the public function protect_identifiers add $item = strtoupper($item); Result:

    public function protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
     {
      $item = strtoupper($item); //linha inserida
    
      if ( ! is_bool($protect_identifiers))
      {
     [...]