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

How to implement a custom Model class

Hello,

I am trying to get Phalcon works wit Oracle DB, but could not install PDO_OCI.

So I tried to implement Database Adapter myself.

I tried looking for Phalcon\Mvc\Model class in source code (https://github.com/phalcon/cphalcon/tree/master/phalcon/mvc/model), but amazing, I could not find something like 'model.zep' file.

This is how I do it:

1, Implement a custom DB adapter:

class Oci8Oracle implements \Phalcon\Db\AdapterInterface

2, In app/config/services.php

$di->setShared('db', function () use ($config) {
    $dbConfig = $config->database->toArray();
    return new \Phalcon\Db\Adapter\Oci8Oracle($dbConfig);
});

3, Create a Model that extends Phalcon\Mvc\Model class

class Table1 extends \Phalcon\Mvc\Model

If I use a non-static method inisde Table1 class, I have access to the adapter:

public function nonStaticMethod(){
        $dbAdapter = $this->getDI()->get('db');
        // Now I can play with the DB Adapter
        // And return something fun
    }

My question is: If I write a static method in class Table1 (like find and findFirst...), how can I access the dbApdater? Could not call getDI()->get('db') because it is not in an object context.

P/S: I know that this is less convenient than PDO, but my requirement is not much about interacting with DB.

Thank you!

You're able to access the DI container statically via

\Phalcon\Di::getDefault();

//eg.
\Phalcon\Di::getDefault()->get('db');

Oh yeah, it works. Thank you very much !