Phalcon: Database sub-Library, read/write separation, load Balancing system method execution sequence

Source: Internet
Author: User

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:

    • Initialize () is called only once per request

    • Perform an initialization task for each instance created with the new Onconstruct ()

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.