Create a New Project with PHPCR-ODM

1.3 version
Maintained Unmaintained
1.2

Create a New Project with PHPCR-ODM

This article will show you how to create a new Symfony project from the Symfony Standard Edition using PHPCR-ODM instead of (or in addition to) the Doctrine ORM.

It is assumed that you have installed composer.

Note

This walkthrough is intended to get you off the ground quickly, for more detailed documentation on integrating the PHPCR-ODM bundle see the documentation for the DoctrinePHPCRBundle.

General Instructions using Jackalope Doctrine DBAL

The Jackalope Doctrine DBAL backend will use Doctrine DBAL to store the content repository.

Step 1: Create a new Symfony project with composer based on the standard edition:

1
$ composer create-project symfony/framework-standard-edition <your project name>/ --no-install

Step 2: Add the required packages to composer.json:

1
2
3
4
5
6
7
8
9
{
    ...
    "require": {
        ...
        "doctrine/phpcr-bundle": "~1.2",
        "doctrine/phpcr-odm": "~1.2",
        "jackalope/jackalope-doctrine-dbal": "~1.1,>=1.1.2"
    }
}

Step 3: (optional) Remove the Doctrine ORM:

  • Remove the doctrine\orm package from composer.json;
  • Remove the orm section from app/config/config.yml.

Step 4: Add the DoctrinePHPCRBundle to the AppKernel:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Doctrine\Bundle\PHPCRBundle\DoctrinePHPCRBundle(),
        );

        // ...
    }
}

Step 5: Modify parameters.yml.dist, adding the required PHPCR-ODM settings:

1
2
3
4
5
6
7
8
9
# app/config/parameters.yml.dist
parameters:
    # ...
    phpcr_backend:
        type: doctrinedbal
        connection: default
    phpcr_workspace: default
    phpcr_user: admin
    phpcr_pass: admin

Note

You are modifying parameters.yml.dist and not paramaters.yml. This is because the Standard Edition will use this file as a template when updating the configuration.

Step 6: Add the Doctrine PHPCR configuration to the main application configuration:

  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    # ...
    doctrine_phpcr:
       # configure the PHPCR session
       session:
           backend: "%phpcr_backend%"
           workspace: "%phpcr_workspace%"
           username: "%phpcr_user%"
           password: "%phpcr_pass%"
       # enable the ODM layer
       odm:
           auto_mapping: true
           auto_generate_proxy_classes: "%kernel.debug%"
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services">
        <config xmlns="http://doctrine-project.org/schema/symfony-dic/odm/phpcr">
            <session backend="%phpcr_backend%"
                workspace="%phpcr_workspace%"
                username="%phpcr_user%"
                password="%phpcr_pass%"
            />
    
            <odm auto-mapping="true"
                auto-generate-proxy-classes="%kernel.debug%"
            />
        </config>
    </container>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    $container->loadFromExtension('doctrine_phpcr', array(
        'session' => array(
            'backend' => '%phpcr_backend%',
            'workspace' => '%phpcr_workspace%',
            'username' => '%phpcr_username%',
            'password' => '%phpcr_password%',
        ),
        'odm' => array(
            'auto_mapping' => true,
            'auto_generate_proxy_classes' => '%kernel.debug%',
        ),
    ));
    

Step 7: Run composer update:

1
$ composer update

After installing the packages composer will ask you to confirm or modify the default parameters defined in parameters.yml.dist and then generate the parameters.yml file.

Your should now be all set to start using PHPCR-ODM in your project!

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.