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

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.



32.2k
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

yes it is working

Java. Java decreased in popularity by about 6,000 job postings in 2018 compared to 2017, but is still extremely well-established. ... Python. Python grew in popularity by about 5,000 job postings over 2017. ... JavaScript. ... C++ ... C# ... PHP. ... Perl.