Shanty_Mongo::getReadConnection PHP Method

getReadConnection() public static method

Get a read connection
public static getReadConnection ( string $connectionGroupName = 'default' ) : Shanty_Mongo_Connection
$connectionGroupName string The connection group name
return Shanty_Mongo_Connection
    public static function getReadConnection($connectionGroupName = 'default')
    {
        $connectionGroup = static::getConnectionGroup($connectionGroupName);
        if ($connectionGroupName == 'default' && count($connectionGroup->getSlaves()) === 0 && count($connectionGroup->getMasters()) === 0) {
            // Add a connection to localhost if no connections currently exist for the default connection group
            $connectionGroup->addMaster(new Shanty_Mongo_Connection('127.0.0.1'));
        }
        if (!($connection = $connectionGroup->getReadConnection($connectionGroupName))) {
            require_once 'Shanty/Mongo/Exception.php';
            throw new Shanty_Mongo_Exception("No read connection available for the '{$connectionGroupName}' connection group");
        }
        return $connection;
    }

Usage Example

Esempio n. 1
0
 /**
  * Fetch an instance of MongoDb
  * 
  * @param boolean $writable
  * @return MongoDb
  */
 public function _getMongoDb($writable = true)
 {
     if (is_null($this->getConfigAttribute('db'))) {
         require_once 'Shanty/Mongo/Exception.php';
         throw new Shanty_Mongo_Exception('Can not fetch instance of MongoDb. Document is not connected to a db.');
     }
     if ($writable) {
         $connection = Shanty_Mongo::getWriteConnection($this->getConfigAttribute('connectionGroup'));
     } else {
         $connection = Shanty_Mongo::getReadConnection($this->getConfigAttribute('connectionGroup'));
     }
     return $connection->selectDB($this->getConfigAttribute('db'));
 }
All Usage Examples Of Shanty_Mongo::getReadConnection