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

No support for additional connections #3

Open
devinim opened this issue Aug 20, 2020 · 1 comment
Open

No support for additional connections #3

devinim opened this issue Aug 20, 2020 · 1 comment

Comments

@devinim
Copy link

devinim commented Aug 20, 2020

When it comes to an ORM, its nice to have multiple connection support.

@bahadirbirsoz
Copy link
Owner

Well first, thanks for your contribution.

In most applications one connection will be enough. So I have no intention to change the default use case. On the other it is a nice-to-have feature to be able handle multiple database connection.

Currently, connection information is obtained from $_ENV superglobal. To handle multiple connections, we would need to change the way it gets the connection information. I am thinking of abstracting $_ENV completely from connection class.

Currently Connection class implements Singleton. There is a static property named $instance. This connection can be considered as default. For other connections it would be a viable solution to extend this class. So the case would be something like this (combining the idea with issue #4 )

class MyDefaultConnection extends \BbOrm\Connection\Mysql
{
    use \BbOrm\DefaultConnectionTrait();
    // ... to be implemented
}
class MySecondaryConnection extends \BbOrm\Connection\SqLite
{
    // ... to be implemented
}

On the other hand, In the models, it get connection from Connection::getInstance static method implemented in \BbOrm\Model. To use some models with non-default connection, in the model, override getConnection method in the Model class. Implementing an abstract parent class in-between \BbOrm\Model and \Project\SomeCustomModel would be an event better solution.

So the use case would be something like :

namespace \MyProject\Models\FirstDatabase;
abstract class Model extends \BbOrm\Model 
{

    public static function getInstance()
    {
        \BbOrm::Connection::getInstance('first_database_connection_key');
    }

}
namespace \MyProject\Models\FirstDatabase;

class Post extends Model 
{   
    public $id;
    public $title;
    public $created_at;
}

Any ideas?

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

2 participants