×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Roman Ignatov
Added: Nov 22, 2016 4:40 PM
Views: 5
Tags: phalcon
  1. <?php
  2.  
  3. use Phalcon\Mvc\Model;
  4.  
  5. class Robots extends Model
  6. {
  7.     /**
  8.      * Dynamically selects a shard
  9.      *
  10.      * @param array $intermediate
  11.      * @param array $bindParams
  12.      * @param array $bindTypes
  13.      */
  14.     public function selectReadConnection($intermediate, $bindParams, $bindTypes)
  15.     {
  16.         // Check if there is a 'where' clause in the select
  17.         if (isset($intermediate['where'])) {
  18.  
  19.             $conditions = $intermediate['where'];
  20.  
  21.             // Choose the possible shard according to the conditions
  22.             if ($conditions['left']['name'] == 'id') {
  23.                 $id = $conditions['right']['value'];
  24.  
  25.                 if ($id > 0 && $id < 10000) {
  26.                     return $this->getDI()->get('dbShard1');
  27.                 }
  28.  
  29.                 if ($id > 10000) {
  30.                     return $this->getDI()->get('dbShard2');
  31.                 }
  32.             }
  33.         }
  34.  
  35.         // Use a default shard
  36.         return $this->getDI()->get('dbShard0');
  37.     }
  38. }