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);
?>
The Mongo class
Увод
The connection point between MongoDB and PHP.
This class is used to initiate a connection and for database server commands. A typical use is:
<?php
$m = new Mongo(); // connect
$db = $m->selectDatabase(); // get a database object
?>
Синтаксис за класове
Mongo
Mongo
{
/* Methods */
__construct
([ string $server = NULL
[, boolean $connect = TRUE
[, boolean $persistent = FALSE
[, boolean $paired = FALSE
]]]] )
}Съдържание
- Mongo::close — Closes this database connection
- Mongo::connect — Connects to a database server
- Mongo::connectUtil — Connects with a database server
- Mongo::__construct — Creates a new database connection object
- Mongo::dropDB — Drops a database
- Mongo::forceError — Creates a database error on the database [deprecated]
- Mongo::lastError — Check if there was an error on the most recent db operation performed [deprecated]
- Mongo::pairConnect — Connects to paired database server
- Mongo::pairPersistConnect — Creates a persistent connection with paired database servers
- Mongo::persistConnect — Creates a persistent connection with a database server
- Mongo::prevError — Checks for the last error thrown during a database operation [deprecated]
- Mongo::repairDB — Repairs and compacts a database
- Mongo::resetError — Clears any flagged errors on the connection [deprecated]
- Mongo::selectCollection — Gets a database collection
- Mongo::selectDB — Gets a database
- Mongo::__toString — String representation of this connection
Fausto Vanin @faustovanin
09-Feb-2011 12:31
markh789 at gmail dot com
07-Jan-2011 10: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
}
?>
