Zend Framework 2 + Doctrine ODM
Zend Framework 2 (ZF2) has finally reached stable. just a few weeks after starting a new project where I plan hitch the NoSQL bandwagon and tryout MongoDB. reading this particular FAQ on Zend Framework’s website..
We currently plan to fully support Zend Framework 1 until at least early 2014, including maintenance and security updates.
I guess I better start learning the latest version of this Framework. okay here goes.
- Same as the previous version of ZF, we need to setup the VirtualHost..
<VirtualHost *:80>
ServerAdmin me@bigwisu.com
ServerName zf2odm.local
DocumentRoot /Users/wisu/Sites/zf2odm/public
<Directory "/Users/wisu/Sites/zf2odm/public">
allow from all
Options +Indexes
</Directory>
SetEnv APPLICATION_ENV "development"
ErrorLog "logs/zf2odm-error_log"
CustomLog "logs/zf2odm-access_log" common
</VirtualHost>
- Get the ZF2 Skeleton
$ cd /path/to/zf2/project
$ git clone git://github.com/zendframework/ZendSkeletonApplication.git .
- Edit the composer.json file, my setup looks like this
{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for ZF2",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"zf2"
],
"homepage": "http://framework.zend.com/",
"minimum-stability": "dev",
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.0.2",
"doctrine/doctrine-mongo-odm-module": "dev-master"
}
}
You must specify “minimum-stability” to dev, and add the “doctrine/doctrine-mongo-odm-module” repository. the default zendframework 2.* does not install. forcing version 2.0.2 seems to do the trick.. after editing, simply run
php composer.phar install
and it will install the framework along with all the dependencies required to access MongoDB..
Now we have Zend Framework 2 up and running.
- Before continuing I think it is best to verify that we already have a MongoDB server running and make sure that PHP can talk to MongoDB. I’m not going to talk too detail about this.. googling will surely give better results.
- Tell ZF2 that we have Doctrine and Doctrine MongoDB ODM Modules. edit config/application.config.php. my setup looks like
<?php
return array(
"modules" => array(
"Application",
"DoctrineModule",
"DoctrineMongoODMModule",
),
"module_listener_options" => array(
"config_glob_paths" => array(
"config/autoload/{,*.}{global,local}.php",
),
"module_paths" => array(
"./module",
"./vendor",
),
),
);
- Configure Doctrine ODM. copy the sample config file to the autoload directory
cp vendor/doctrine/doctrine-mongo-odm-module/config/module.doctrine-mongo-odm.local.php.dist config/autoload/module.doctrine-mongo-odm.local.php
Edit the file, my setup looks like so..
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
'dbname' => 'zf2odm',
'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' => 'zf2odm',
'filters' => array()
)
),
'driver' => array(
'odm_default' => array(
'drivers' => array(
'Application\Document' => 'aplikasi'
)
),
'aplikasi' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
'module/Application/src/Application/Document'
)
)
),
'documentmanager' => array(
'odm_default' => array(
'connection' => 'odm_default',
'configuration' => 'odm_default',
'eventmanager' => 'odm_default'
)
),
'eventmanager' => array(
'odm_default' => array(
'subscribers' => array()
)
),
),
);
- Create Hydrator and Proxy directories
mkdir -p data/DoctrineMongoODMModule/Hydrator
mkdir -p data/DoctrineMongoODMModule/Proxy
Make sure they are writable by your web server.
- Define Document mapping. here for example is the User mapping i have. specified in module/Application/src/Application/Document/User.php
namespace Application\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** @ODM\Document(collection="user") */
class User
{
/** @ODM\Id */
private $id;
/** @ODM\Field(type="string") */
private $name;
/**
* @return the $id
*/
public function getId() {
return $this->id;
}
/**
* @return the $name
*/
public function getName() {
return $this->name;
}
/**
* @param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param field_type $name
*/
public function setName($name) {
$this->name = $name;
}
}
- To create an entry in MongoDB, we specify in our controller what mapping is to be used, here we will create a new user.
use Application\Document\User;
then sample code.
$dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
$user = new User();
$user->setName("Gembul");
$dm->persist($user);
$dm->flush();
in the an action to write user “Gembul” to our MongoDB’s user Collection. results is as the following.