Oliverde8's blue Website


Sonata, MongDB & Symfony 3.3 on PHP7.1

I wanted to know if it was possible to use Sonata with mMngodb. After some research I found out that it was indeed possible. 

If there is one thing I love it's discovering new things, trying out new things. There is so much I would like todo, sadly weekends are to shorty for all the experiments I wish to run.
I would like to write about those experiments more often, but it takes quite a lot of time to write these articles; so usually I don't write much about it. 

But getting mongodb sf3.3 with sonata working without a mysql server has been slightly more complicated then expected, because the documentation was not that complete and there isn't any stable release of some of the bundles needed.

Before setting sail, I will not go into the details, I assume you have already installed a sonata with doctrine odm and that you are familliar with the process. 

Also I used many elements from the sonata project user bundle installation guide.

What you need to installed

I did my install on a PHP 7.1 (thanks docker to make it so easy). But it should work the same on 7.0 normally. 

You will of course need a brand new SF3.3, install with the fallowing packages : 


We need doctrine to get our database sorted out.

Doctrine doesen't work with the latest php 7+ mongodb driver, which means we need another package to add compatibility


We also need sonata for our BO.

Sadly we have to use dev-master and not any tags. There are fixes for SF3.3 and also for PHP7.1 that's not available in any tags. Latest version will simply not work. This is where I lost most of my time as I tried not to use any dev versions.

Mongdb ACL bundle

Well normally acl are handled in the orm database. But as we wish to use mongodb. Again we will need a bundle in dev-master

This bundles composer.json says it isn't compatible with php 7.1. The issue is this bundle seems to be abandoned. But I couldn't find anything similar not abandoned. So let's use composer with --ignore-platform-reqs to get this bundle installed.

End result

"require": {
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/mongodb-odm": "^1.1.6",
    "alcaeus/mongo-php-adapter": "^1.1.2",
    "incenteev/composer-parameter-handler": "^2.0",
    "sensio/distribution-bundle": "^5.0.19",
    "sensio/framework-extra-bundle": "^3.0.2",
    "sonata-project/doctrine-mongodb-admin-bundle": "dev-master",
    "sonata-project/user-bundle": "dev-master",
    "symfony/monolog-bundle": "^3.1.0",
    "symfony/polyfill-apcu": "^1.0",
    "symfony/swiftmailer-bundle": "^2.3.10",
    "symfony/symfony": "3.3.*",
    "twig/twig": "^1.0||^2.0",
    "predis/predis": "^1.1.1",
    "pwalkow/mongodb-acl-bundle": "dev-master"

Other bundles such as FOSUserBundle has been installed at this stage. Let's no start configuring our SF.

Getting truely started

Enable the proper bundles. 

Modify your AppKernel to add the fallowing bundles.

// Dependency bundles
new \Knp\Bundle\MenuBundle\KnpMenuBundle(),
new \FOS\UserBundle\FOSUserBundle(),
new \PWalkow\MongoDBAclBundle\MongoDBAclBundle(),

// Sonata bundles
new \Sonata\CoreBundle\SonataCoreBundle(),
new \Sonata\BlockBundle\SonataBlockBundle(),
new \Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),
new \Sonata\AdminBundle\SonataAdminBundle(),
new \Sonata\DoctrineMongoDBAdminBundle\SonataDoctrineMongoDBAdminBundle(),
new \Sonata\UserBundle\SonataUserBundle('\'FOSUserBundle\''),

Configuration (app/config/config.yml)

Remove the symfony default doctrine orm configuration and add mongodb configuration

            server: "%mongodb_server%"
            options: {}
    default_database: "%mongodb_database%"
            auto_mapping: true

Configure the acl storage :

        default_database: %mongodb_database%

Let's note that I have used a intermediate variable mongodb_database & server that will need to be set in the parameters.yml

We need to configure fos user bundle as well

        address : [email protected]
        sender_name: toto
    db_driver:      mongodb
    firewall_name:  main
    user_class:     Sonata\UserBundle\Entity\BaseUser

        group_class:   Sonata\UserBundle\Entity\BaseGroup
        group_manager: sonata.user.mongodb.group_manager

        user_manager: sonata.user.mongodb.user_manager

We are going to lake some changes here later. For now let's continue with our configuration. 

Finally we need to configure sonata

    default_contexts: [cms]
        # enable the SonataAdminBundle block
            contexts: [admin]

    security_acl: true
    manager_type: mongodb

This are quick configurations to get things going, you will need to make changes. 

Routing (app/config/routing.yml)

We simply add the sonata routes. 

    resource: "@SonataUserBundle/Resources/config/routing/sonata_security_1.xml"

    resource: "@SonataUserBundle/Resources/config/routing/sonata_resetting_1.xml"
    prefix: /resetting

    resource: "@SonataUserBundle/Resources/config/routing/sonata_profile_1.xml"
    prefix: /profile

    resource: "@SonataUserBundle/Resources/config/routing/sonata_registration_1.xml"
    prefix: /register

    resource: "@SonataUserBundle/Resources/config/routing/sonata_change_password_1.xml"
    prefix: /profile

    resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml'
    prefix: /admin

    resource: '@SonataUserBundle/Resources/config/routing/admin_resetting.xml'
    prefix: /admin/resetting

You could also use the fos routes, both will work.

We are now missing the security configuration.

Security (app/config/security.yml)

            - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  # if you are using acl then this line must be commented

            id: fos_user.user_manager

        # Disabling the security for the web debug toolbar, the profiler and Assetic.
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # -> custom firewall for the admin area of the URL
            pattern:            /admin(.*)
            context:            user
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
                path:           /admin/logout
                target:         /admin/login
            anonymous:          true
        # -> end custom configuration

        # default login area for standard users
        # This firewall is used to handle the public login area
        # This part is handled by the FOS User Bundle
            pattern:             .*
            context:             user
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
            logout:             true
            anonymous:          true

        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        # Admin login page needs to be accessed without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

        provider: mongodb_acl_provider

Basically this is the exact same as what has been done in the sonata documentation : https://sonata-project.org/bundles/user/3-x/doc/reference/installation.html#integrating-the-bundle-into-the-sonata-admin-bundle

With the diffrence of the acl provider being 


At this point everyhing is nearly working, but if you try to open a page sf it going to tell you that mongodb_acl_provider is not know. We need to register a new service. To have it simple let's put this in the app/config/services.yml file

Adding Mongodb acl service

A simple service declaration is sufficient : 

        parent: doctrine_mongodb.odm.security.acl.provider
        abstract: false
        autowire: false
        autoconfigure: false
        public: true

At this stage we are nearly ready. 

Extending Sonata User bundle

We need to generate the proper entities. 

php app/console sonata:easy-extends:generate SonataUserBundle -d src

Edit the AppKernetl to add our new bundle

new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),

And update fos configuration to use the new entity, we only edit the fallowing configurations. 

    user_class:     Application\Sonata\UserBundle\Entity\User
        group_class:   Application\Sonata\UserBundle\Entity\Group


We are nearly there, let's now create our mongodb indexes. 

Preparing indexes

bin/console doctrine:mongodb:schema:update

We also need to init the acl's; 

docker-compose exec php bin/console init:acl:mongodb

And you are good to go. 

Errors I encountered

Sadly you are going to encounter a few errors in the sonata interface when trying to edit the users. 

I had to create a getGenderList method in the ApplicationSonataUserBudnles/Mode/User 

    public function getGenderList()
        return ['male' => 'Male', 'female' => 'Female'];

Last I had an error with the groups not being able to render propelry, and ended up removing it from the admin form of the users. 


Using Sonata on a orm project is quite easy, once all is installed everything works fine and the tutorials are straight forward. But on mongodb it's not so, there a re quite a few things not documented. The acl provider is the most annoying one of the bunch. Even in the latest symfony 3.3 documentation this is not propelry documented as it links to a unmaintained version. I believe majority of the install used mongodb & mysql together and therfore doesen't require acl stored in mongo. 

I would like to point that it has been a month already since I actually did this and some new versions might be out there not requiring you to use the dev-master. I haven't had time to check. 

Globally this install works well, but the sonata mongodb admin bundle is still not 100%. For exemple for some reasons embeded documens displayed as a table works great but the standard display is ugly.  I ended up using the views of the admin bundle for collections which works and looks much better.