Symfony通过DoctrineMongoDBODM访问MongoDB

 

一、概述

开发环境:PHP 5.3.0+ / Symfony 1.4.4 / DoctrineMongoDBODM 1.0.0 / MongoDB 1.4.4

阅读提示:本文需读者了解以下内容 PHP命名空间、Symfony框架、MongoDB、Doctrine ODM(下载ODM)
Win平台MongoDB的安装与使用可参看:《PHP操作MongoDB

 

二、配置与开发步骤
1、首先在命令行下创建Symfony项目,假设主目录为:ROOT,项目名为MG
>cd ROOT
>php lib/vendor/symfony/data/bin/symfony generate:project MG --orm=none

 

2、将下载的Doctrine ODM解压至ROOT/lib/vendor/doctrine/lib目录下,eg:ROOT/lib/vendor/doctrine/lib/Doctrine/ODM

 

3、在ROOT/lib下创建documents存放数据集模型,类似与表模型,同时创建Proxy的两个目录,分别为:generate和proxies(ROOT/lib/generate/proxies)

 

4、修改项目配置文件ROOT/config/ProjectConfiguration.class.php,代码如下:

require_once dirname(__FILE__) .'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php'; sfCoreAutoload::register(); require_once __DIR__ .'/../lib/vendor/doctrine/lib/Doctrine/Common/ClassLoader.php'; use Doctrine/Common/CLassLoader; class ProjectConfiguration extends sfProjectConfiguration { public function setup() { $classLoader = new ClassLoader('Doctrine/ODM', __DIR__.'/../lib/vendor/doctrine/lib'); $classLoader->register(); $classLoader = new ClassLoader('Doctrine/Common', __DIR__.'/../lib/vendor/doctrine/lib'); $classLoader->register(); $classLoader = new ClassLoader('Documents', __DIR__.'/../lib/documents'); $classLoader->register(); } }  

 

5、在命令行下采用Symfony命令创建应用frontend和模块mg

>php symfony generate:app frontend

>php symfony generate:module frontend mg

 

6、在ROOT/lib/documents目录下创建三个文件:MongoDB操作类(Documents.class.php),用户数据集类似于用户表模型(Users.class.php),博客文章数据集类似于博客文章表模型(BlogPost.class.php),内容如下:

//Documents.class.php use Doctrine/Common/Annotations/AnnotationReader, Doctrine/ODM/MongoDB/DocumentManager, Doctrine/ODM/MongoDB/Mongo, Doctrine/ODM/MongoDB/Configuration, Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver; class Documents { public static $dm = null; public static function create() { if (is_null(self::$dm)) { $config = new Configuration(); $config->setProxyDir(__DIR__.'/../generate/proxies'); $config->setProxyNamespace('Proxies'); $reader = new AnnotationReader(); $reader->setDefaultAnnotationNamespace('Doctrine/ODM/MongoDB/Mapping//'); $config->setMetadataDriverImpl(new AnnotationDriver($reader, __DIR__)); self::$dm = DocumentManager::create(new Mongo(), $config); } return self::$dm; } } //Users.class.php class Users { public $id; public $name; public $email; public $posts = array(); } //Blogpost.class.php class BlogPost { public $id; public $title; public $body; public $createdAt; }  

7、修改模块mg的action类(ROOT/apps/frontend/modules/mg/actions/actions.class.php)

class mgActions extends sfActions { public function executeIndex(sfWebRequest $request) { //新建数据集User对象 $user = new Users(); $user->name = 'caleng'; $user->email = '[email protected]'; Documents::create()->persist($user); //新建数据集Blogpost对象 $post = new BlogPost(); $post->title = 'My First Blog Post'; $post->body = 'MongoDB + Doctrine 2 ODM'; $post->createdAt = date('Y-m-d'); $user->posts[] = $post; Documents::create()->flush(); //查找用户blog $userId = $user->id; $loadedUser = Documents::create()->find('Users', $userId); foreach ($loadedUser->posts as $post) { echo $post->title . PHP_EOL; echo $post->body . PHP_EOL; echo 'By ' . $loadedUser->name . ' on ' . $post->createdAt; } } } 

至此Symfony操作MongoDB的简易示例已完成,启动MongoDB与Web服务器,在浏览器输入http://localhost/ROOT/web/index.php/mg,即可看到效果。深入内容请待后文或参看本文的教程链接。

 

 

你可能感兴趣的:(WEB,PHP,mongodb,classloader,doctrine,function,php,web服务)