コード例 #1
0
 /**
  * Get the Mongo database connection object. Uses your
  * settings from `conf/config.php` for the connection
  * info (`host`, `name`, `user`, `pass`, and `set_name`).
  * The host and database name are required, but authentication
  * (`user` and `pass`) settings and replica set name
  * (`set_name`) are optional.
  */
 public static function get_connection()
 {
     $conf = conf('Mongo');
     if (!self::$conn) {
         if (isset($conf['user'])) {
             $connstr = 'mongodb://' . $conf['user'] . ':' . $conf['pass'] . '@' . $conf['host'];
         } else {
             $connstr = $conf['host'];
         }
         try {
             if (isset($conf['set_name'])) {
                 self::$conn = new Mongo($connstr, array('replicaSet' => $conf['set_name']));
             } else {
                 self::$conn = new Mongo($connstr);
             }
         } catch (Exception $e) {
             self::$error = $e->getMessage();
         }
     }
     return self::$conn;
 }