Phalcon: Database sub-Library, read/write separation, load Balancing system method execution sequence
Using namespaces to differentiate between different database instances, the corresponding code is structurally distinct from the directory, and the base class is responsible for initializing the connection in the same directory. Connect multiple DB services injected from initialization
Hidden rules:
namespace Company\Models\Notification;
/** * Class Basemodel * * BeforeSave () and Afterfetch () are used in pairs to automatically convert data when reading and writing data. * For example, automatic execution of serialize unserialize * * Save () occurs when event invocation order is * Initialize, * onconstruct, * beforevalidation, * BEFOREVALIDATIONONCR Eate, * aftervalidationoncreate, * aftervalidation, * beforesave, * beforecreate, * aftercreate, * afterSave, * * @package Company\models\notification */class Basemodel extends \phalcon\mvc\model{/** *-Initialize () will only be called once per request * -Subclass must call parent class method *-perform initialization task for each instance created with Onconstruct () */Public Function Initialize () {$this->setc Onnectionservice (' db_notification '); $this->setconnectionservice (' Node1 '); $this->setconnectionservice (' Node2 '); Real-world scenarios may use Mysqlnd_ms extensions or Haproxy//only demonstrate read load balancing a thought//$dbSlave = [' Node1 ', ' node2 ', ' node3 ']; $key = Array_rand ($dbSlave); $db = $dbSlave [$key]; $this->setreadconnectionservice ($DB); //$this->setreadconnectionsErvice (' DBRead '); $this->setwriteconnectionservice (' dbwrite '); }}
Phalcon: Database sub-Library, read/write separation, load Balancing system method execution sequence