Search code examples
oracleoracle-call-interfaceyii2

Yii Active Record with Oracle is not working correctly


I am trying to use Yii active record with Oracle database but it seems Active record lacks support to Oracle databases the very first problem is quoting the table names. Consider this model

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Tblleaves extends ActiveRecord
{
    public static function tableName()
    {
        return 'tblleaves';
    }
}

When I try to fetch all the records in my action with

$leaves = Tblleaves::find()->all();

It raises the exception with message 'table or view does not exist (/tmp/pear/download/PDO_OCI-1.0/oci_statement.c:142) The SQL being executed was: SELECT * FROM "tblleaves"'

If I query leaves like this

$connection = new \yii\db\Connection([
    'dsn' => 'oci:dbname=//10.0.0.200:1521/orcl',
    'username' => 'JISPBILCORBILLINGPRD501',
    'password' => 'JISPBILCORBILLINGPRD501',
]);
$connection->open();
$command = $connection->createCommand('SELECT * FROM tblleaves');
$posts = $command->queryAll();
echo '<pre>'; print_r($posts); exit;

It works prefecly fine. I later commented the line of QueryBuilder

$tables = $this->quoteTableNames($tables, $params);

It also worked fine, but then ActiveRecord was not able to get the scheme of table so $leave->property was giving an error of undefined property. What is going on with active record


Solution

  • According to this SO answer:

    Putting double-quotes around an identifier in Oracle causes Oracle to treat the identifier as case sensitive rather than using the default of case-insensitivity.

    I've just tried this on an Oracle 11g instance and it is correct for that version.

    You should confirm the case of the table name in the database and then modify the return value of tableName() to match.