module-db icon indicating copy to clipboard operation
module-db copied to clipboard

feat: allow developer to define own connection

Open gjorgic opened this issue 9 months ago • 0 comments

Allow user to create connection outside of module-db, or even use connection from different module;

You could define class MyDb with method getConnection which will return instance of \Codeception\Lib\Driver\Db class

class MyDB
{
    protected static $connection;

    public static function setConnection($connection)
    {
        self::$connection = $connection;
    }

    public static function getConnection($databaseKey, $databaseConfig, $dbModule)
    {
        return new class (self::$connection) extends \Codeception\Lib\Driver\Db {
            public function __construct(protected $connection)
            {
            }

            public function getDbh(): \PDO
            {
                return $this->connection->getNativeConnection();
            }
        };
    }
}

gjorgic avatar Oct 02 '23 04:10 gjorgic