Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to use existing "db" instead opening new one #33

Open
zeecher opened this issue Nov 5, 2018 · 0 comments
Open

Possibility to use existing "db" instead opening new one #33

zeecher opened this issue Nov 5, 2018 · 0 comments

Comments

@zeecher
Copy link

zeecher commented Nov 5, 2018

I need option to use existing "db" connection. I'm using sqlite in-memory option in my unit tests, in-memory option is not shareable between apps. That's why i have to share my apps db connection with yarak. Here is my workaround in ConnectionResolver class

<?php

namespace Yarak\DB;

use Phalcon\Exception;
use Phalcon\Di;

class ConnectionResolver
{
    /**
     * Get connection to database.
     *
     * @param array $dbConfig
     *
     * @throws Exception
     *
     * @return \Phalcon\Db\Adapter\Pdo
     */
    public function getConnection(array $dbConfig)
    {

        $di = DI::getDefault();

        if($di->has('db'))
            return $di->getShared('db');

        $dbClass = sprintf('\Phalcon\Db\Adapter\Pdo\%s', $dbConfig['adapter']);

        if (!class_exists($dbClass)) {
            throw new Exception(
                sprintf('PDO adapter "%s" not found.', $dbClass)
            );
        }

        unset($dbConfig['adapter']);

        return new $dbClass($dbConfig);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant