Home  >  Article  >  Backend Development  >  cakephp 与 mongodb 集成教程_PHP教程

cakephp 与 mongodb 集成教程_PHP教程

WBOY
WBOYOriginal
2016-07-21 14:53:37768browse

没有mongodb的测试环境的可以在本地安装个mongodb服务,这里有mongodb在windows下的图文安装教程。

php 默认没有开启mongodb扩展,需要手动到官网上下载mongo扩展,在这里找到适合你系统的mongo扩展,将其解压放入到php环境指定的ext目录下,同时在php.ini文件中加入

extension=php_mongo.dll

重启apache等服务器后生效.

从github上下载cakephp与mongodb的datasouce,安装在app/plugins/目录下

PS:没有git的同志也不用担心,可以直接下载

下载完成后就可以在database.php中配置mongodb:

var $mongo = array(
‘datasource’ => ‘mongodb.mongodbSource’,
‘database’ => ‘testmongo’,
‘host’ => ‘localhost’,
‘port’ => 27017
);

可以创建一个model在控制器中使用它:

//mongb.php

class Mondb extends AppModel {
var $name = ‘Mondb’;
var $primaryKey = ‘_id’;
var $useDbConfig = ‘mongo’;

function schema() {
$this->_schema = array(
‘_id’ => array(‘type’ => ‘integer’, ‘primary’ => true, ‘length’ => 40),
‘a’ => array(‘type’ => ‘string’),
‘b’ => array(‘type’ => ‘integer’),
);
return $this->_schema;
}

}
?>

那么在控制器中就可以操作mongodb了:

function mongo(){
$this -> loadModel(‘Mondb’);
$res = $this -> Mondb -> save(array(“a”=”test mongodb”,”b”=>time()));
$res = $this -> Mondb -> find(‘all’);
pr($res);
exit;
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/364753.htmlTechArticle没有mongodb的测试环境的可以在本地安装个mongodb服务,这里有mongodb在windows下的图文安装教程。 php 默认没有开启mongodb扩展,需要手动到官网上...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn