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):
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:
Add this to the configuration array:
1 return [
// ...
// 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\AnnotationDriver',
'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
Create a managed document class
Create your first Doctrine ODM managed document class in
module/Application/src/Application/Document/Message.php
:
1 <?php
namespace Application\Document;
use 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
32
33
34
35
36
37
38
39
40
41
Test the newly created document
To test your Doctrine ODM configuration, replace the indexAction in
module/Application/src/Application/Controller/IndexController.php
:
1 <?php
//...
use Application\Document\Message;
//...
public function indexAction()
{
$message = new Message();
$message->setText("Hello Doctrine!");
$dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
$dm->persist($message);
$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
The dumped variable should contain a new generated id: