Developing Web Apps with
 Symfony2, Doctrine and
       MongoDB
Who Am I?

• Software   Engineer at TechJini Solutions
• Working    with PHP/Symfony for last 4 years
• @kertz   on Twitter and Github
The Whys?

• Why   Symfony2?
• Why   Doctrine?
Symfony2
• Made   of reusable components
•Acommunity that built Doctrine, Behat,
 Composer, Assetic and more
• Thebackbone for future versions of Drupal &
 phpBB
Doctrine

• Highly   configurable
• Supports
        MySQL, PgSQL, Oracle, SQlite,
 MongoDB and CouchDB
• Doctrine   Query Language
Installation

• ComposerDependency Manager (http://
 getcomposer.org)


$ curl -s http://getcomposer.org/installer | php


$ php composer.phar create-project symfony/framework-
standard-edition sf-mongo 2.1.x-dev
Adding MongoDB
                                           composer.json
{
    "require": {
        "doctrine/mongodb-odm-bundle": "3.0.*"
    },
    "minimum-stability": "dev"
}

$ composer.phar update doctrine/mongodb-odm-bundle

                                        AppKernel.php
Configuration
                                                config.yml
doctrine_mongodb:
    connections:
        default:
            server: mongodb://localhost:27017
            options: {}
    default_database: sfmongo
    document_managers:
        default:
            auto_mapping: true
Bundles


• Why   Bundles?
• Creating   your own bundles
 $ app/console generate:bundle
Register Bundles

               AppKernel.php
What are we building?

•A   simple image sharing app
• Store/Retrieve   images in/from GridFS
• Allow   comments for images
The Document schema for images
                            Image.php
Controller for uploading images
                            ImageController.php
The template for uploading images


                                new.html.twig
Retrieving the images


                   ImageController.php
In Retrospect

•A   form to upload an image and add caption
• Store   images in MongoDB with GridFS
• Retrieve   images
• That   was quick, wasn’t it?
The Document schema for comments


                         Comment.php
Add a reference to comments

                              Image.php
Controller for comments
                          ImageController.php
Viewing comments for images
                        ImageController.php




               comments.html.twig
In Retrospect (again)

• Allow   comments for image
• Show    comments for images by id
Repositories & DQL

• Create   custom repositories

• Using   Doctrine Query Builder
Repository for comments

                 CommentRepository.php
Set repository location




                          Comment.php
Calling Repository functions
                        CommentController.php
Bundles to watch

• SonataAdminBundle       http://github.com/sonata-project/SonataAdminBundle



• FOSUserBundle   http://github.com/FriendsOfSymfony/FOSUserBundle



• FOQElasticaBundle     http://github.com/Exercise/FOQElasticaBundle
The End. Questions?

Mongo db bangalore 2012