Home > php教程 > php手册 > body text

cakephp 与 mongodb 集成教程

WBOY
Release: 2016-06-13 11:36:33
Original
984 people have browsed it

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

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

extension=php_mongo.dll
Copy after login

重启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;
}

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!