ThinkCMF是一款基于ThinkPHP+MySQL開(kāi)發(fā)的中文內(nèi)容管理框架孕讳。(摘自官方)
1.下載ThinkCMFX2.2.3
2.安裝nginx+php+mysql基本環(huán)境
3.安裝mongodb
4.php-mongo模塊安裝
5.將下載的thinkcmf包,解壓后厂财,傳到nginx的發(fā)布目錄 芋簿。(/www/html/)
6.安裝thinkcmf
ThinkCMF支持mongodb的配置及基本的增刪改查操作
ThinkCMF支持mongodb的配置及基本的增刪改查操作
ThinkCMF支持mongodb的配置及基本的增刪改查操作
7.安裝成功后璃饱,可以看到成功頁(yè)面提示,有訪問(wèn)前臺(tái)頁(yè)面和后臺(tái)頁(yè)面的地址荚恶。
-----------------------------------------------------------------------------------
下面來(lái)使ThinkCMF對(duì)mongodb也可以來(lái)操作。
1.在配置文件中添加mongodb的配置信息谒撼。(/www/html/data/conf/db.php , /www/html/為web的發(fā)布目錄)
'mongo' => array(
//'配置項(xiàng)'=>'配置值'
'DB_TYPE' => 'mongo', // 數(shù)據(jù)庫(kù)類(lèi)型
'DB_HOST' => '127.0.0.1', // 服務(wù)器地址
'DB_NAME' => 'thinkcmf', // 數(shù)據(jù)庫(kù)名
'DB_USER' => 'thinkcmf', // 用戶名
'DB_PWD' => 'thinkcmf', // 密碼
'DB_PORT' => '27017', // 端口
'DB_PREFIX' => 'cmf_',
),
2.添加連接mongodb的方法 (/www/html/application/Common/Common/function.php)
function getMongoDb(){
$mongodb = C ( 'mongo' );
return $mongodb;
}
3.修改文件 Mongo.class.php 路徑 /www/html/simplewind/Core/Library/Think/Db/Driver/Mongo.class.php
修改前:73行~77行
if(!empty($db)) { // 傳人Db則切換數(shù)據(jù)庫(kù)
// 當(dāng)前MongoDb對(duì)象
$this->_dbName = $db;
$this->_mongo = $this->_linkID->selectDb($db);
}
修改后:
if(!empty($db)) { // 傳人Db則切換數(shù)據(jù)庫(kù)
// 當(dāng)前MongoDb對(duì)象
$this->_dbName = $db;
$this->_mongo = $this->_linkID->selectDb($db);
}else{
$db=$this->config['database'];
$this->_dbName = $db;
$this->_mongo = $this->_linkID->selectDb($db);
}
添加了一個(gè)else的情況,否則會(huì)在連接mongodb的時(shí)候冻河,頁(yè)面會(huì)報(bào)錯(cuò)
Call to a member function selectCollection() on a non-object
4.同樣修改 文件 Mongo.class.php 路徑 /www/html/simplewind/Core/Library/Think/Db/Driver/Mongo.class.php
修改前:418行
$resultSet = iterator_to_array($_cursor);
修改后:
$resultSet = iterator_to_array($_cursor,false);
主要是解決在查詢用 find()和select()的時(shí)候,輸出為數(shù)組茉帅。
4.在 /www/html/application/Portal/Controller/ 目錄下新建 TestmongodbController.class.php,內(nèi)容如圖:
ThinkCMF支持mongodb的配置及基本的增刪改查操作
幾個(gè)基本的增刪改查的方法:
public function add(){
$data = array(
'name'=>'yycomsx',
'sex'=>'男',
'address'=>'陜西西安',
);
$this->member_model->add($data);
echo "添加成功锭弊!";
}
public function edit(){
$where = array(
'name'=>'yycomsx',
);
$data = array(
'address'=>'陜西西安11111',
);
$this->member_model->where($where)->save($data);
echo "修改成功!";
}
public function find() {
$where = array(
'name'=>'yycomsx',
);
$member = $this->member_model->where($where)->find();
print_r($member);
exit;
}
public function delete(){
$where = array(
'name'=>'yycomsx',
);
$this->member_model->where($where)->delete();
echo "刪除成功樱蛤!";
}
基于主鍵刪除
$array = array(
? '_id' => new \MongoId('5b62a5c7babf4a0d378b4567'),
);
? ? ? $this->member_model->where($array)->delete();