downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Mongo::close> <Classes internes
[edit] Last updated: Fri, 10 Feb 2012

view this page in

La classe Mongo

(No version information available, might only be in SVN)

Introduction

La connexion entre MongoDB et PHP.

Cette classe est utilisée pour créer et gérer les connexions. Un exemple classique est :

<?php

$m 
= new Mongo(); // connexion
$db $m->foo// lecture de l'objet de base de données "foo"

?>

Voir Mongo::__construct() ainsi que la section sur les connections pour plus d'informations sur la création de connexions.

Synopsis de la classe

Mongo {
/* Constantes */
const string VERSION ;
const string DEFAULT_HOST = "localhost" ;
const int DEFAULT_PORT = 27017 ;
/* Champs */
public boolean $Mongo->connected = FALSE ;
public string $status = NULL ;
protected string $server = NULL ;
protected boolean $persistent = NULL ;
/* Méthodes */
public bool Mongo::close ( void )
public bool Mongo::connect ( void )
protected bool Mongo::connectUtil ( void )
public Mongo::__construct ([ string $server = "mongodb://localhost:27017" [, array $options = array("connect" => TRUE) ]] )
public array Mongo::dropDB ( mixed $db )
public MongoDB Mongo::__get ( string $dbname )
public array Mongo::getHosts ( void )
public static int Mongo::getPoolSize ( void )
public string Mongo::getSlave ( void )
public bool Mongo::getSlaveOkay ( void )
public array Mongo::listDBs ( void )
public array Mongo::poolDebug ( void )
public MongoCollection Mongo::selectCollection ( string $db , string $collection )
public MongoDB Mongo::selectDB ( string $name )
publicstaticbool Mongo::setPoolSize ( int $size )
public bool Mongo::setSlaveOkay ([ bool $ok = true ] )
public string Mongo::switchSlave ( void )
public string Mongo::__toString ( void )
}

Constantes pré-définies

Constantes Mongo

Mongo::VERSION
Version du drvier PHP. Peut être préfixé avec un "+" ou un "-" si la version se trouve entre 2 versions.
Mongo::DEFAULT_HOST
"localhost"
Hôte sur lequel on doit se connecter si aucun n'est fourni.
Mongo::DEFAULT_PORT
27017
Port sur lequel on doit se connecter si aucun n'est fourni.

Champs

status
Si c'est une connexion persistante, si la connexion a été créée pour cet objet ou si elle est réutilisée. Si la connexion n'est pas persistante, ce champ est NULL.

Voir aussi

Documentation de MongoDB » concernant les connections.

Sommaire



add a note add a note User Contributed Notes Mongo
Fausto Vanin @faustovanin 10-Feb-2011 04:31
For those who are concerned on parsing JSON associative arrays from queries, this class could be useful. You just have to extend it and call parent constructor and it gets the job done.
It automatically initializes all your object attributes getting values from the array.

<?php

   
#doc
    #    classname:    MongoClass
    #    scope:        PUBLIC
    #
    #/doc
   
   
class MongoClass
   
{
       
#    internal variables
       
protected $id;
       
       
#    Constructor
       
function __construct ($attList = array())
        {
           
$reflection = new ReflectionObject($this);

            foreach (
$attList as $attName => $attValue)
            {
               
$attObj = $reflection->getProperty($attName);
               
$attObj->setAccessible(true);
               
$attObj->setValue($this, $attValue);
            }
        }
       
###   
   
   
}
   
###
       
class A extends MongoClass {
                private
$name;
                private
$value;
                private
$weight;

                public function
__construct($attList) {
                       
parent::__construct($attList);
                }
        }

       
$attList = array(
               
"name" => "Beer",
               
"value" => "Delicious",
               
"weight" => 15.2
       
); //This is your JSON object associative aray

       
$a = new A($attList);

?>
markh789 at gmail dot com 08-Jan-2011 02:32
Here is a simple connection function :)

<?php
function MongoConnect($username, $password, $database, $host) {
   
$con = new Mongo("mongodb://{$username}:{$password}@{$host}"); // Connect to Mongo Server
   
$db = $con->selectDB($database); // Connect to Database
}
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites