本文整理匯總了PHP中MongoDB::setSlaveOkay方法的典型用法代碼示例。如果您正苦於以下問題:PHP MongoDB::setSlaveOkay方法的具體用法?PHP MongoDB::setSlaveOkay怎麽用?PHP MongoDB::setSlaveOkay使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類MongoDB
的用法示例。
在下文中一共展示了MongoDB::setSlaveOkay方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: setSlaveOkay
/**
* Set whether secondary read queries are allowed for this database.
*
* This method wraps setSlaveOkay() for driver versions before 1.3.0. For
* newer drivers, this method wraps setReadPreference() and specifies
* SECONDARY_PREFERRED.
*
* @see http://php.net/manual/en/mongodb.setreadpreference.php
* @see http://php.net/manual/en/mongodb.setslaveokay.php
* @param boolean $ok
* @return boolean Previous slaveOk value
*/
public function setSlaveOkay($ok = true)
{
if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
return $this->mongoDB->setSlaveOkay($ok);
}
$prevSlaveOkay = $this->getSlaveOkay();
if ($ok) {
// Preserve existing tags for non-primary read preferences
$readPref = $this->getReadPreference();
$tags = !empty($readPref['tagsets']) ? $readPref['tagsets'] : array();
$this->mongoDB->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, $tags);
} else {
$this->mongoDB->setReadPreference(\MongoClient::RP_PRIMARY);
}
return $prevSlaveOkay;
}