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

List of options for setting connection to the database (documentation issue)

Hello,

Couple of days ago I started going through documentation of Phalcon. I want to use it for a certain project. Somewhere in the tutorial of INVO is written, that a connection to the database can be set up with multiple options (as I understood it). Here's the quote: " could do extra actions such as adding a logger, a profiler or change the adapter, setting it up as you want." So I figured that I just need to add additional options in the passed array while setting up $di->set('db'... I have been looking for the list of options but cannot find it. I went through https://docs.phalcon.io/en/latest/api/Phalcon_Db_Adapter_Pdo_Mysql.html, as well as https://docs.phalcon.io/en/latest/api/Phalcon_Db_Adapter_Pdo.html but still nothing. Can anyone point me to proper documentation, where I can find this?

Thanks



8.1k
edited Mar '14

You don't need to read the documentation about this :) See PHP documentation. For example, you may investigate, how this problem solved in Phalcon Developer Tools (Phalcon\Mvc\Model\Migration.php ++69)


    public static function setup($database)
    {

        if ( ! isset($database->adapter))
            throw new \Phalcon\Exception('Unspecified database Adapter in your configuration!');

        $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $database->adapter;

        if ( ! class_exists($adapter))
            throw new \Phalcon\Exception('Invalid database Adapter!');

        $configArray = $database->toArray();
        unset($configArray['adapter']);
        self::$_connection = new $adapter($configArray);
        self::$_databaseConfig = $database;

        if ( \Phalcon\Migrations::isConsole() ) {
            $profiler = new Profiler();

            $eventsManager = new EventsManager();
            $eventsManager->attach('db', function ($event, $connection) use ($profiler) {
                if ($event->getType() == 'beforeQuery') {
                    $profiler->startProfile($connection->getSQLStatement());
                }
                if ($event->getType() == 'afterQuery') {
                    $profiler->stopProfile();
                }
            });

            self::$_connection->setEventsManager($eventsManager);
        }
    }