Read and write separation

Register read and write link configuration information

First, we need to register to read/write the information of the two links according to Configuration Information Registration.

The main code is probably as follows

DbManager::getInstance()->addConnection($con, 'read');
DbManager::getInstance()->addConnection($con2, 'write');

Specify to use the link

There are two ways to use it. You can choose according to your needs.

Mainly use the connection() method provided by AbstractModel

Function connection(string $name, bool $isTemp = false)

The second parameter needs to be passed as true, indicating temporary use, otherwise it is considered to be fixed use (the effect is equivalent to defining the connectionName attribute in the class)

Model inheritance definition

Class Test extends AbstractModel{
     /** This is because you are not using the default configuration link name, so you need to specify */
     Protected $connectionName = 'write';
     /** get method uses read link */
     Public function get($where = null, bool $returnAsArray)
         $this->connection('read', true);
         Return parent::get($where, $returnAsArray);

External use