Monday, February 5, 2018

Migrate Coneigniter from MySQL to Oracle

Hi,

I was migrating an existed system developed using Codeiginter 2.1.4 and MySQL database into Oracle database.

Definitely there was several things to migrate in DML and queries such as MySQL built-in functions that are not existed in Oracle like (LIMIT, CONCAT, DATE_ADD, DATE_FORMAT etc..) .. but the most annoying thing was case-sensitive issue in tables and columns names.

As an Oracle developer, I was never cared about columns and tables characters case in queries .. but I fond that Codeigniter has another opinion.

For example if I have a database column called "ID" (in capital letters), the following PHP result-set will not work:

$user = $this->db->get_where('users_table', array('username' => $username, 'password' => $password, 'state' => 1, 'deleted' => 0));                
        if($user->num_rows() == 1)
        {
            $user = $user->row();             
            $user_id = $user->id;

        }

Because column name is "ID" not "id" thus I couldn't get the column value, if it changed to $user_id = $user->ID; it will work.

There's two solutions:

  • Either user same case for both database and PHP code.

You can change the Codeigniter system and make all attributes in lower case:
I quote from thread:

To get object attributes lowercase do next:
1. go to db_result.php near line 75
2. find method result_object()
3. change:

Code:
while ($row = $this->_fetch_object())
        {
            $this->result_object[] = $row;
       
        }
with:
Code:
while ($row = $this->_fetch_object())
        {
            $new_row = array();
            foreach ($row as $key => $value) {
                    $new_row[strtolower($key)] = $value;
            }
            $this->result_object[] = (object) $new_row;
       
        }

And for getting result as array:

1. open oci8_result.php
2. find method result_array()
3. change:

Code:
while ($this->_fetch_assoc($row))
        {
           
            $this->result_array[] = $row;
        }

with:

Code:
while ($this->_fetch_assoc($row))
        {
           
            $new_row = array();
            foreach ($row as $key => $value) {
                    $new_row[strtolower($key)] = $value;
            }
           
            $this->result_array[] = $new_row;
        }