Heim  >  Artikel  >  Datenbank  >  ZendFramework2 与MongoDB的整合

ZendFramework2 与MongoDB的整合

WBOY
WBOYOriginal
2016-06-07 15:22:151276Durchsuche

从网上找了很多文章,先是直接搜关键字找zf2与mongoDB的文章,然后回到源头先学习了一下mongoDB是什么,以及纯PHP环境下怎么用,又从github上找了几个mongoDB的zf2模块,还翻墙找了不少东西,再经过调试终于有了成果。下面是我的具体步骤: 1、先从官网下载m

从网上找了很多文章,先是直接搜关键字找zf2与mongoDB的文章,然后回到源头先学习了一下mongoDB是什么,以及纯PHP环境下怎么用,又从github上找了几个mongoDB的zf2模块,还翻墙找了不少东西,再经过调试终于有了成果。下面是我的具体步骤:

1、先从官网下载mongoDB,选择和操作系统对应的版本,下载的是个zip文件,解压后,找到里面的bin文件夹,把所有文件复制到f:\tools\mongodb里,新建data文件夹,

这是文件目录

\

\

2、因为我的php版本是5.4以上的,php扩展文件下里已经存在php_mongo.dll,因此只需修改php.ini,去掉 ;extension=php_mongo.dll 前面的 ; 然后重启就可以了,如果没有这个dll,从https://s3.amazonaws.com/drivers.mongodb.org/php/index.html下载最新的就行了。通过phpinfo()函数查看是否出现mongoDb

3、打开cmd命令窗口,cd到上述目录,使用命令

\

\

回车后加载很多信息,在浏览器中输入地址“http://localhost:27017/”,若出现如下所示提示则安装成功

\

\

需要注意的是这个命令窗口不要关,否则就访问不到了,然后可以参考这个地址学习一下mengoDB的php用法

4、从https://github.com/phly/PhlyMongo上下载全部代码,加压后把PhlyMongo-master文件夹复制到项目的vendor下,这是文件目录

\

5、找到config\application.config.php,把‘Phlymongo’加入到modules中,如图所示,注意‘PhlyMongo’一定要放在你要引用mongoDB的模块的前面,我就是因为这个问题耽误了半小时,在这里我要在Book模块中使用,所以放在了它的前面\

5、打开Applicaiton模块下的config\module.config.php,把下面一段代码复制到里面

'mongo'=>array(
			'server'         => 'mongodb://localhost:27017',
			'server_options' => array('connect' => true)
	)
如图所示

 

\

6、在你要引用mongoDB的模块中,比如我要在Book模块中使用mongoDB,则找到Book\module.php,把这一段代码假如里面

 

use PhlyMongo\MongoConnectionFactory;
	public function getServiceConfig(){
     	return array(
     		'factories' => array(
            	'Book\Mongo'           => function ($services) {
                	$config = $services->get('config');
                	$config = $config['mongo'];
                	$factory = new MongoConnectionFactory($config['server'], $config['server_options']);
               	    return $factory->createService($services);
            	}));
     }

 

7、然后在controller里就可以调用了,我是这样引用的,

 

protected $mongoTable;
	protected $_result;
	public function demoAction() {	
		$result = $this->_result;
		try {
			$connection=$this->getMongoConnection();
			$database=$connection->selectDB("myblogsite");//这是事先创建的一个database
			$collection=$database->selectCollection("articles");//这是事先创建的一个collection
			$cursor=$collection->find();
			while ($cursor->hasNext()){
				$article = $cursor->getNext();
			}
			print_r($article);
			$connection->close();
			exit();
		} catch (\Exception $e) {
			$result['msg'] = $e->getMessage();
		}
	
	}
    public function getMongoConnection(){
    	if (! $this->mongoConnection) {
    		$sm = $this->getServiceLocator();
    		$this->mongoConnection= $sm->get('Book\Mongo');
    	}
    	return $this->mongoConnection;
    }


8、最后输出结果如下,里面的数据是我原先insert的

 

\

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:使用ASMACFS文件系统Nächster Artikel:redis设置key过期时间