【发布时间】:2012-04-03 09:45:54
【问题描述】:
我有以下代码可以连接到 MongoDB:
try {
$m = new Mongo('mongodb://'.$MONGO['servers'][$i]['mongo_host'].':'.$MONGO['servers'][$i]['mongo_port']);
} catch (MongoConnectionException $e) {
die('Failed to connect to MongoDB '.$e->getMessage());
}
// select a database
$db = $m->selectDB($MONGO["servers"][$i]["mongo_db"]);
然后我创建了一个 PHP 类,我想在 Mongo 中检索/更新数据。我不知道如何访问之前创建的 Mongo 连接。
class Shop {
var $id;
public function __construct($id) {
$this->id = $id;
$this->info = $this->returnShopInfo($id);
$this->is_live = $this->info['is_live'];
}
//returns shop information from the database
public function returnShopInfo () {
$where = array('_id' => $this->id);
return $db->shops->findOne($where);
}
}
代码是这样的:
$shop = new Shop($id);
print_r ($shop->info());
【问题讨论】: