We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Oracle Database Connection error

I have been trying to connect to Oracle Database using the following code:

$di = new \Phalcon\DI\FactoryDefault();

// Set up the database service
$di->set('db', function(){
    return new \Phalcon\Db\Adapter\Pdo\Oracle(array(
        "dbname" => "localhost/xe",
        "username" => "username",
        "password" => "password",
    ));
});

// Bind the DI to the application
$app->setDI($di);

However, I continue to get the following error:

phql Table "robots" doesn't exist on database when dumping meta-data for Robots

Any idea where I might be going wrong? Any help is appreciated. Thanks a lot.

edited Nov '16

You wrote a wrong dbname/host.

Must be separately host and dbname. see doc: https://docs.phalcon.io/en/latest/reference/tutorial.html#setting-a-database-connection

 //Set the database service
$di->set('db', function(){
        return new \Phalcon\Db\Adapter\Pdo\Oracle(array(
            "host" => "localhost",
            "username" => "username",
            "password" => "password",
            "dbname" => "xe"
        ));
});

@ptechie

Your connection to oracle is correct.

PDO_Oracle dont need host parameter.

I generally use in connection tns_name, check this

$connection = new \Phalcon\Db\Adapter\Pdo\Oracle(array(
    'dbname' => '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=xe)(FAILOVER_MODE=(TYPE=SELECT)(METHOD=BASIC)(RETRIES=20)(DELAY=5))))',
    'username' => 'PHALCON',
    'password' => 'Phalcon',
    'charset' => 'AL32UTF8',
    'options' => [PDO::ATTR_CASE => PDO::CASE_LOWER, PDO::ATTR_PERSISTENT => TRUE,],
));

Oracle need the schema parameter in their models to function properly.

you can set on your models as follows:

class Robots extends \Phalcon\Mvc\Model {

    public function initialize() {
        $this->setSchema('PHALCON');
        $this->setSource('ROBOTS');
    }

}

Another point of attention, are the names of objects which are usually configured by default in uppercase.

Sry for my bad Brazinglish :)



3.4k
Accepted
answer

@Marcio :Thanks a lot for your response. I realised I was getting an error because my tables and schema were in uppercase whereas I had used lower case for the tables in my code. Now I am able to connect to Oracle Database. Thanks a lot for your help. :)

Did you have to install pdo_oci to get it working? It is experimental according to the PHP manual. Has anyone used oci8 library with phalcon?