ThinkCMF支持mongodb的配置及基本的增刪改查操作

ThinkCMF是一款基於ThinkPHP+MySQL開發的中文內容管理框架。(摘自官方)

1.下載ThinkCMFX2.2.3

2.安裝nginx+php+mysql基本環境

3.安裝mongodb

4.php-mongo模塊安裝

5.將下載的thinkcmf包,解壓後,傳到nginx的發佈目錄 。(/www/html/)

6.安裝thinkcmf

ThinkCMF支持mongodb的配置及基本的增刪改查操作

ThinkCMF支持mongodb的配置及基本的增刪改查操作

ThinkCMF支持mongodb的配置及基本的增刪改查操作

7.安裝成功後,可以看到成功頁面提示,有訪問前臺頁面和後臺頁面的地址。

-----------------------------------------------------------------------------------

下面來使ThinkCMF對mongodb也可以來操作。

1.在配置文件中添加mongodb的配置信息。(/www/html/data/conf/db.php , /www/html/為web的發佈目錄)

'mongo' => array(

//'配置項'=>'配置值'

'DB_TYPE' => 'mongo', // 數據庫類型

'DB_HOST' => '127.0.0.1', // 服務器地址

'DB_NAME' => 'thinkcmf', // 數據庫名

'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則切換數據庫

// 當前MongoDb對象

$this->_dbName = $db;

$this->_mongo = $this->_linkID->selectDb($db);

}

修改後:

if(!empty($db)) { // 傳人Db則切換數據庫

// 當前MongoDb對象

$this->_dbName = $db;

$this->_mongo = $this->_linkID->selectDb($db);

}else{

$db=$this->config['database'];

$this->_dbName = $db;

$this->_mongo = $this->_linkID->selectDb($db);

}

添加了一個else的情況,否則會在連接mongodb的時候,頁面會報錯

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()的時候,輸出為數組。

4.在 /www/html/application/Portal/Controller/ 目錄下新建 TestmongodbController.class.php,內容如圖:

ThinkCMF支持mongodb的配置及基本的增刪改查操作

幾個基本的增刪改查的方法:

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 "刪除成功!";

}

訪問地址分別為:

http://192.168.199.122/index.php?m=Testmongodb&a=add 添加

http://192.168.199.122/index.php?m=Testmongodb&a=edit 修改

http://192.168.199.122/index.php?m=Testmongodb&a=find 查詢

http://192.168.199.122/index.php?m=Testmongodb&a=delete 刪除


分享到:


相關文章: