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 删除


分享到:


相關文章: