| +34 608 61 64 10



Zend2-Doctrine2-MongoDb (Create Post)

This is the second post of my blog test using Zend 2, Doctrine 2 and MongoDB. You'll find how to create a post adding the flashmessanger options to show the message return everytime submitting form.
The blog is based on these previous Zend 2, Doctrine 2 and MongoDB installation and CRUD examples and you can clone or download the zip from my github account.

Installation and Configuration
CRUD - Insert/create document
CRUD - Select document
CRUD - Update/Delete document
BLOG - Login

and of course you can find all the methods and all the documentation at the Doctrine Oficial link.

In order to create a new post, I'll create the module "Post" and put its mongodb model is Post/Document/Post.php
The most important features in my code is the create a common layouts for all the modules. The main layout is called layout.phtml and these "partial" layouts flashmessage.phtml, footer.phtml, header.phtml, menu.phtml, modal.phtml. These latters are included from the layout.phtml.
The directory of the layout is on Application/view/layout/partials as you can see in the git repository.

The other relevant features are:
1) Modify config/autoload/module.doctrine-mongo-odm.local.php
Adding the reference to the new model

'driver' => array(
    'odm_default' => array(
	'drivers' => array(
		'User\Document' => 'mongoPb',
                'Post\Document' => 'mongoPb'

2) Using the mongo type to stored and retrieve the value as numeric index array.
Adding the type collection into Post/Document/Post.php when you define the category field

    /** @ODM\Field(type="string") */
    private $texto;    

    /** @ODM\Field(type="collection") */
    private $listCategories; 


So you can store the categories correctly:

	"_id" : ObjectId("56010855e0cf20c82726f247"),
	"idAuthor" : "55eb0e51e0cf20f70e8b4567",
	"title" : "title prueba final",
	"texto" : "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque",
	"listCategories" : [
		"categoria 1",
		" categoria 2",
		" categoria 3"

Note: You can find more detail using type MongoDB with Doctrine in this stackoverflow link.

You can find all code in my github account, including the manage of MongoDB using Doctrine 2 and the error message using the flashmessage