php-orm-pdo icon indicating copy to clipboard operation
php-orm-pdo copied to clipboard

How i can use master and slave database

Open sandeep-nambiar opened this issue 6 years ago • 2 comments

sandeep-nambiar avatar Oct 15 '18 12:10 sandeep-nambiar

I would say you can simply create 2 different classes and overwrite the connection part on each of them, and then inherit from the one that uses the connect you want?

msoffredi avatar Oct 15 '18 12:10 msoffredi

Yes, @msoffredi. Thanks for your help.

I think that something like this should work:

MasterModel.php

use SimpleORM\core\model\Model;

class MasterModel extends Model
{
    public $db_config = [
        'db_host' => '192.168.1.1',
        'db_name' => 'test',
        'db_user' => 'root',
        'db_pass' => ''
    ];
}

SlaveModel.php

use SimpleORM\core\model\Model;

class SlaveModel extends Model
{
    public $db_config = [
        'db_host' => '192.168.1.1',
        'db_name' => 'test',
        'db_user' => 'root',
        'db_pass' => ''
    ];
}

And them you use with your own models:

use YourNamespace\AppModel;

class Example extends MasterModel
{
    public $table = 't_user';
    public $pk    = 'user_id';

    public function getAll()
    {
        return $this->findAll();
    }
}

.....

Let me know if you need more help.

arojunior avatar Oct 15 '18 18:10 arojunior