Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Invo App - Database Service Registration: why unset the adapter?

Hi, I am working myself through the current Invo app implementation and don't get why the adapter is getting unset in Services.php after the connection string is created:

    protected function initSharedDb()
    {
        $config = $this->get('config')->get('database')->toArray();
        $dbClass = 'Phalcon\Db\Adapter\Pdo\\' . $config['adapter'];
        unset($config['adapter']);
        return new $dbClass($config);
    }

Would be nice if someone could enlighten me, thanks.



20.3k
Accepted
answer

Because it is used to create the FQN of the class and the Mysql adapter it would be redundant to say "you must be an Mysql adapter"

You can also use the Factory PDO service to avoid having to delete anything, you can use it as follows:

use Phalcon\Db\Adapter\Pdo\Factory;

 protected function initSharedDb()
{
    $config = $this->get('config')->get('database')->toArray();
    return Factory::load($config);
}

Good luck