MongoClient::setReadPreference PHP Method

setReadPreference() public method

Set the read preference for this connection
public setReadPreference ( string $readPreference, array $tags = null ) : boolean
$readPreference string -
$tags array -
return boolean -
    public function setReadPreference($readPreference, array $tags = null)
    {
        if ($new_preference = self::_validateReadPreference($readPreference, $tags)) {
            $this->readPreference = $new_preference;
        }
        return (bool) $new_preference;
    }

Usage Example

 /**
  * Connects to our database
  */
 public function connect()
 {
     // We don't need to throw useless exceptions here, the MongoDB PHP Driver has its own checks and error reporting
     // Yii will easily and effortlessly display the errors from the PHP driver, we should only catch its exceptions if
     // we wanna add our own custom messages on top which we don't, the errors are quite self explanatory
     if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
         $this->_mongo = new Mongo($this->server, $this->options);
         $this->_mongo->connect();
         if ($this->setSlaveOkay) {
             $this->_mongo->setSlaveOkay($this->setSlaveOkay);
         }
     } else {
         $this->_mongo = new MongoClient($this->server, $this->options);
         if (is_array($this->RP)) {
             $const = $this->RP[0];
             $opts = $this->RP[1];
             if (!empty($opts)) {
                 // I do this due to a bug that exists in some PHP driver versions
                 $this->_mongo->setReadPreference(constant('MongoClient::' . $const), $opts);
             } else {
                 $this->_mongo->setReadPreference(constant('MongoClient::' . $const));
             }
         }
     }
 }
All Usage Examples Of MongoClient::setReadPreference