You are browsing a version that is no longer maintained.

User Guide

This guide helps you to create a Laminas application with Doctrine Mongo ODM integration. If you’re new to Laminas, please read the Laminas documentation before you continue.

Install Composer

Install composer via curl -s http://getcomposer.org/installer | php (on windows, download http://getcomposer.org/installer and execute it with PHP)

Laminas Skeleton Application

Create a new Skeleton application with Composer:

$ composer create-project laminas/laminas-mvc-skeleton doctrine-odm-tutorial

Grab a coffee and wait for composer to finish installing your new Laminas Application. Now you can start up your application with the php built-in web server:

$ cd doctrine-odm-tutorial
$ php -S 0.0.0.0:8080 -t public/ public/index.php

For detailed instructions on installing the Laminas MVC Skeleton Application follow this link.

Install Doctrine Mongo ODM Module

Install DoctrineMongoODMModule :

$ composer require doctrine/doctrine-mongo-odm-module

Open config/application.config.php in your editor and add following keys to your modules (in this order):

1'DoctrineModule', 'DoctrineMongoODMModule',
2

Copy vendor/doctrine/doctrine-mongo-odm-module/config/module.doctrine-mongo-odm.local.php.dist into your application’s config/autoload directory, rename it to module.doctrine-mongo-odm.local.php and make the appropriate changes.

Create the directories my/project/directory/data/DoctrineMongoODMModule/Proxy and my/project/directory/data/DoctrineMongoODMModule/Hydrator and make sure your application has write access to them.

Configure your Application module

Open module/Application/config/module.config.php and add the namespace to the top of the file:

1<?php namespace Application;
2
3

Add this to the configuration array:

1return [ // ... // to register classes in the "/Document" folder of any module, // you can copy&paste this block to a module config without modifying it. 'doctrine' => [ 'driver' => [ __NAMESPACE__ . '_driver' => [ 'class' => \Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver::class, 'paths' => [ __DIR__ . '/../src/' . __NAMESPACE__ . '/Document', ], ], 'odm_default' => [ 'drivers' => [ __NAMESPACE__ . '\Document' => __NAMESPACE__ . '_driver', ], ], ], ], ];
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Create a managed document class

Create your first Doctrine ODM managed document class in module/Application/src/Application/Document/Message.php. Here, we are using the Mongo ODM attribute syntax, which requires PHP 8.0 or newer.

1use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; #[ODM\Document] class Message { #[ODM\Id] protected $id; #[ODM\Field(type: "string")] protected $text; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getText() { return $this->text; } public function setText($text) { $this->text = $text; } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

Test the newly created document

To test your Doctrine ODM configuration, replace the indexAction in module/Application/src/Application/Controller/IndexController.php and add the document manager to the constructor:

1use Application\Document\Message; use Laminas\Mvc\Controller\AbstractActionController; class IndexController extends AbstractActionController { public function __construct(private DocumentManager $dm) {} public function indexAction() { $message = new Message(); $message->setText("Hello Doctrine!"); $this->dm->persist($message); $this->dm->flush(); var_dump($message); return new ViewModel(); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Next, you need to set up a factory for your controller in module/Application/src/Controller/IndexControllerFactory.php, to boostrap it with the instance of Doctrine's document manager:

1use Psr\Container\ContainerInterface; class IndexControllerFactory { public function __invoke(ContainerInterface $container) { return new IndexController($container->get('doctrine.documentmanager.odm_default')); } }
2
3
4
5
6
7
8
9

Lastly, wire everything together by configuring your newly created factory for your controller in module/Application/config/module.config.php:

1// ... 'controllers' => [ 'factories' => [ Controller\IndexController::class => Controller\IndexControllerFactory::class, ], ], // ...
2
3
4
5
6
7

When accessing the index controller, the dumped variable should contain a new generated id:

1object(Application\Document\Message)[252] protected 'id' => string '546a6bf935568055040041a9' (length=24) protected 'text' => string 'Hello Doctrine!' (length=15)
2
3