For high performance and scalable application NoSQL database like mongoDB is the best match for Zend. There are dozens of MongoDb available for Zend but no doubt that Docrine ODM is the beast and reliable. But for total beginners like me it was not easy to make mongoDB work with Zend. In this post I have described the method I approached.
Prerequisite:
1. You have installed Zend server.
Read Installing Zend Server in 3 steps [Ubuntu]
2. You have already deployed your Zend Framework 2 Application.
Read Deploying Zend Frameork 2 “Hello World” Application [Ubuntu]
1. Go to the root of your zf2 application, open composer.json file and the following lines of code:
"minimum-stability": "alpha", "require": { "doctrine/doctrine-mongo-odm-module": "dev-master" }
Now your composer.json file will look something like this:
{ "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause", "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.2.*" }, "minimum-stability": "alpha", "require": { "doctrine/doctrine-mongo-odm-module": "dev-master" } }
Alternatively you can create new composer.json file with following content:
{ "minimum-stability": "alpha", "require": { "doctrine/doctrine-mongo-odm-module": "dev-master" } }
2. Install all dependencies using composer:
#php composer.phar install
Please note that sometimes this step take extra long time. Keep Patience.
3. copy vendor/doctrine/doctrine-mongo-odm-module/config/module.doctrine-mongo-odm.local.php.dist to config/autoload and rename it there to module.doctrine-mongo-odm.local.php. Or in single command :
#cp vendor/doctrine/doctrine-mongo-odm-module/config/module.doctrine-mongo-odm.local.php.dist config/autoload/module.doctrine-mongo-odm.local.php
4. create directory data/DoctrineMongoODMModule/Proxy and data/DoctrineMongoODMModule/Hydrator.
#mkdir -p data/DoctrineMongoODMModule/Proxy #mkdir -p data/DoctrineMongoODMModule/Hydrator
If any permission problem occur, make these directory writable by the web server:
#chown -R www-data:www-data data/DoctrineMongoODMModule/Proxy #chown -R www-data:www-data data/DoctrineMongoODMModule/Hydrator
5. Add ‘DoctrineModule’ and ‘DoctrineMongoODMModule’ in the list of module in the file configs/application.config.php :
array( 'Application', 'DoctrineModule', 'DoctrineMongoODMModule', ),
6. Now Open the config file: config/autoload/module.doctrine-mongo-odm.local.php and edit it according to your configuration. Following is my file:
array( 'connection' => array( 'odm_default' => array( 'server' => 'localhost', 'port' => '27017', // 'connectionString' => null, // 'user' => null, // 'password' => null, // 'dbname' => test, // 'options' => array() ), ), 'configuration' => array( 'odm_default' => array( // 'metadata_cache' => 'array', 'driver' => 'odm_default', 'generate_proxies' => true, 'proxy_dir' => 'data/DoctrineMongoODMModule/Proxy', 'proxy_namespace' => 'DoctrineMongoODMModule\Proxy', 'generate_hydrators' => true, 'hydrator_dir' => 'data/DoctrineMongoODMModule/Hydrator', 'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator', // // 'default_db' => test, // // 'filters' => array(), // array('filterName' => 'BSON\Filter\Class'), // // 'logger' => null // 'DoctrineMongoODMModule\Logging\DebugStack' ) ), 'driver' => array( 'odm_driver' => array( 'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver', 'paths' => array(__DIR__ . '/../../module/Application/src/Application/Document') ), 'odm_default' => array( 'drivers' => array( 'Application\Document' => 'odm_driver' ) ), ), 'documentmanager' => array( 'odm_default' => array( // 'connection' => 'odm_default', // 'configuration' => 'odm_default', // 'eventmanager' => 'odm_default' ) ), 'eventmanager' => array( 'odm_default' => array( 'subscribers' => array() ) ), ), );
Please note that as my mongoDB database don’t have any username or password set, I commented it.
That’s all done! Now you are ready to write your application using mongoDB database.
In next post. I will show how to write a simple CRUD application using mongoDB/Zend Framework 2.
This post first appeared on Technohunk.com| Daily Hacks In Plain English!, please read the originial post: here