一、概述
開發環境: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 = 'caleng@caleng.com';
- 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,即可看到效果。深入內容請待後文或參看本文的教程連結。