Symfony通過DoctrineMongoDBODM訪問MongoDB

來源:互聯網
上載者:User

一、概述

開發環境: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,代碼如下:

  1. require_once dirname(__FILE__)  
  2.     .'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';  
  3. sfCoreAutoload::register();  
  4. require_once __DIR__  
  5.     .'/../lib/vendor/doctrine/lib/Doctrine/Common/ClassLoader.php';  
  6. use Doctrine/Common/CLassLoader;  
  7. class ProjectConfiguration extends sfProjectConfiguration  
  8. {  
  9.     public function setup()  
  10.     {  
  11.         $classLoader = new ClassLoader('Doctrine/ODM',   
  12.             __DIR__.'/../lib/vendor/doctrine/lib');  
  13.         $classLoader->register();  
  14.         $classLoader = new ClassLoader('Doctrine/Common',   
  15.             __DIR__.'/../lib/vendor/doctrine/lib');  
  16.         $classLoader->register();  
  17.         $classLoader = new ClassLoader('Documents',   
  18.             __DIR__.'/../lib/documents');  
  19.         $classLoader->register();  
  20.     }  
  21. }  

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),內容如下:

  1. //Documents.class.php   
  2. use Doctrine/Common/Annotations/AnnotationReader,  
  3.     Doctrine/ODM/MongoDB/DocumentManager,  
  4.     Doctrine/ODM/MongoDB/Mongo,  
  5.     Doctrine/ODM/MongoDB/Configuration,  
  6.     Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver;  
  7.       
  8. class Documents  
  9. {  
  10.     public static $dm = null;  
  11.       
  12.     public static function create()  
  13.     {  
  14.         if (is_null(self::$dm)) {  
  15.             $config = new Configuration();  
  16.             $config->setProxyDir(__DIR__.'/../generate/proxies');  
  17.             $config->setProxyNamespace('Proxies');  
  18.             $reader = new AnnotationReader();  
  19.             $reader->setDefaultAnnotationNamespace('Doctrine/ODM/MongoDB/Mapping//');  
  20.             $config->setMetadataDriverImpl(new AnnotationDriver($reader, __DIR__));  
  21.             self::$dm = DocumentManager::create(new Mongo(), $config);  
  22.         }  
  23.         return self::$dm;  
  24.     }  
  25. }  
  26. //Users.class.php   
  27. class Users  
  28. {  
  29.     public $id;  
  30.     public $name;  
  31.     public $email;  
  32.     public $posts = array();  
  33. }  
  34. //Blogpost.class.php   
  35. class BlogPost  
  36. {  
  37.     public $id;  
  38.     public $title;  
  39.     public $body;  
  40.     public $createdAt;  
  41. }  

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

  1. class mgActions extends sfActions  
  2. {  
  3.     public function executeIndex(sfWebRequest $request)  
  4.     {  
  5.         //建立資料集User對象   
  6.         $user = new Users();  
  7.         $user->name = 'caleng';  
  8.         $user->email = 'caleng@caleng.com';  
  9.         Documents::create()->persist($user);  
  10.           
  11.         //建立資料集Blogpost對象   
  12.         $post = new BlogPost();  
  13.         $post->title = 'My First Blog Post';  
  14.         $post->body = 'MongoDB + Doctrine 2 ODM';  
  15.         $post->createdAt = date('Y-m-d');  
  16.         $user->posts[] = $post;  
  17.         Documents::create()->flush();  
  18.           
  19.          //尋找使用者blog   
  20.         $userId = $user->id;  
  21.         $loadedUser = Documents::create()->find('Users', $userId);  
  22.         foreach ($loadedUser->posts as $post) {  
  23.             echo $post->title . PHP_EOL;  
  24.             echo $post->body . PHP_EOL;  
  25.             echo 'By ' . $loadedUser->name . ' on ' . $post->createdAt;  
  26.         }  
  27.     }  
  28. }  

至此Symfony操作MongoDB的簡易樣本已完成,啟動MongoDB與Web伺服器,在瀏覽器輸入http://localhost/ROOT/web/index.php/mg,即可看到效果。深入內容請待後文或參看本文的教程連結。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.