<commit_before>﻿Doctrine 2 is a project that aims to handle the persistence of the domain model in a non-interfering way.
Non-relational or no-sql databases like MongoDB give you flexibility of building data store arround your object model and not vise versa.
You can read more on the initial configuration and setup in [Introduction to MongoDB Object Document Mapper](/projects/mongodb_odm/1.0/docs/reference/introduction/en#introduction)
This section will give you a basic overview of what could be accomplished using Doctrine2 ODM.

## Example Model: Simple Blog

To create the simplest example, let's assume the following:

* Blog has a user.
* Blog user can make blog posts

## A first prototype

For the above mentioned example, something as simple as this could be modeled:

    [php]
    class User
    {
        public $username;
        public $email;
        public $posts = array();
        
    }
    class BlogPost
    {
        public $title;
        public $body;
        public $createdAt;
    }

> **WARNING**
> This is only a prototype, please don't use public properties with Doctrine 2 at all,
> the "Queries for Application Use-Cases" section shows you why. In combination with proxies
> public properties can make up for pretty nasty bugs.

To make the above classes persistent, all we need to do is to add some annotations and identifiers arround them:

    [php]
    /**
     * @Document(database="test_database", collection="users")
     */
    class User
    {
        /**
         * @Id
         */
        public $id;
        /**
         * @String
         */
        public $name;
        /**
         * @String
         */
        public $email;
        /**
         * @ReferenceMany(targetDocument="BlogPost", cascade="all")
         */
        public $posts = array();
        
    }
    /**
     * @Document(database="test_database", collection="blog_posts")
     */
    class BlogPost
    {
        /**
         * @Id
         */
        public $id;
        /**
         * @String
         */
        public $title;
        /**
         * @String
         */
        public $body;
        /**
         * @Date
         */
        public $createdAt;
    }

That's it, we have our models, and we can save and retreive them.
Now all we need to do is to properly instantiate the DocumentManager instance([Introduction to MongoDB Object Document Mapper](/projects/mongodb_odm/1.0/docs/reference/introduction/en#introduction)):

    [php]
    $config = new Configuration();
    $config->setProxyDir('/path/to/generate/proxies');
    $config->setProxyNamespace('Proxies');

    $reader = new AnnotationReader();
    $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
    $config->setMetadataDriverImpl(new AnnotationDriver($reader, __DIR__ . '/Documents'));

    $dm = DocumentManager::create(new Mongo(), $config);

Here is how you would use your models now:

    [php]
    // ...<commit_msg>added section headings<commit_after>﻿Doctrine 2 is a project that aims to handle the persistence of the domain model in a non-interfering way.
Non-relational or no-sql databases like MongoDB give you flexibility of building data store arround your object model and not vise versa.
You can read more on the initial configuration and setup in [Introduction to MongoDB Object Document Mapper](/projects/mongodb_odm/1.0/docs/reference/introduction/en#introduction)
This section will give you a basic overview of what could be accomplished using Doctrine2 ODM.

## Example Model: Simple Blog

To create the simplest example, let's assume the following:

* Blog has a user.
* Blog user can make blog posts

## A first prototype

For the above mentioned example, something as simple as this could be modeled:

    [php]
    class User
    {
        public $username;
        public $email;
        public $posts = array();
        
    }
    class BlogPost
    {
        public $title;
        public $body;
        public $createdAt;
    }

> **WARNING**
> This is only a prototype, please don't use public properties with Doctrine 2 at all,
> the "Queries for Application Use-Cases" section shows you why. In combination with proxies
> public properties can make up for pretty nasty bugs.

## Persistent Models

To make the above classes persistent, all we need to do is to add some annotations and identifiers arround them:

    [php]
    /**
     * @Document(database="test_database", collection="users")
     */
    class User
    {
        /**
         * @Id
         */
        public $id;
        /**
         * @String
         */
        public $name;
        /**
         * @String
         */
        public $email;
        /**
         * @ReferenceMany(targetDocument="BlogPost", cascade="all")
         */
        public $posts = array();
        
    }
    /**
     * @Document(database="test_database", collection="blog_posts")
     */
    class BlogPost
    {
        /**
         * @Id
         */
        public $id;
        /**
         * @String
         */
        public $title;
        /**
         * @String
         */
        public $body;
        /**
         * @Date
         */
        public $createdAt;
    }

That's it, we have our models, and we can save and retreive them.
Now all we need to do is to properly instantiate the DocumentManager instance([Introduction to MongoDB Object Document Mapper](/projects/mongodb_odm/1.0/docs/reference/introduction/en#introduction)):

    [php]
    $config = new Configuration();
    $config->setProxyDir('/path/to/generate/proxies');
    $config->setProxyNamespace('Proxies');

    $reader = new AnnotationReader();
    $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
    $config->setMetadataDriverImpl(new AnnotationDriver($reader, __DIR__ . '/Documents'));

    $dm = DocumentManager::create(new Mongo(), $config);

## Usage

Here is how you would use your models now:

    [php]
    // ...
{'fraction_non_alphanumeric': 0.10015174506828528, 'fraction_numerical': 0.0023604788399932556, 'mean_word_length': 2.3552036199095023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2257715', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1573, 'n_words': 569}