2. Installation

2.1. Install the Bundle with Composer

In your project directory, execute the following command to download the latest stable version of the MultiTenancyBundle:

1
composer require swp/multi-tenancy-bundle

This command requires you to have Composer installed globally. If it’s not installed globally, download the .phar file locally as explained in Composer documentation.

2.2. Enable the bundle and its dependencies

Note

By default Jackalope Doctrine DBAL is required for PHPCR ODM in this bundle. See Choosing a PHPCR Implementation for alternatives.

Enable the bundle and its dependencies (DoctrinePHPCRBundle, DoctrineBundle) by adding the following lines in the app/AppKernel.php file:

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

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Doctrine\Bundle\PHPCRBundle\DoctrinePHPCRBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new SWP\MultiTenancyBundle\SWPMultiTenancyBundle(),
        );

        // ...
    }

    // ...
}

Note

All dependencies will be installed automatically. You will just need to configure the respective bundles.

2.3. Configure the SWPMultiTenancyBundle

Let’s enable PHPCR persistence backend.

  • YAML
    1
    2
    3
    4
    5
    6
    # app/config/config.yml
    swp_multi_tenancy:
        persistence:
            phpcr:
                # if true, PHPCR is enabled in the service container
                enabled: true
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    <!-- app/config/config.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <swp_multi_tenancy>
        <persistence>
            <phpcr>
                <!-- if true, PHPCR is enabled in the service container -->
                <enabled>true</enabled>
            </phpcr>
        </persistence>
    </swp_multi_tenancy>
    

Note

See Configuration Reference for more details.

2.4. DoctrinePHPCRBundle Configuration

See how to set PHPCR Session Configuration.

2.5. Add the domain parameter

Add the following parameter to your parameters file, so the current tenant can be resolved and matched against the configured domain.

1
2
# app/config/parameters.yml
domain: example.com

2.6. Update your database schema

Note

This step assumes you have already configured and created the database.

Execute the following commands in the console:

1
2
3
4
5
php bin/console doctrine:schema:update --force
php bin/console doctrine:phpcr:repository:init
php bin/console swp:organization:create --default
php bin/console swp:tenant:create --default
php bin/console doctrine:phpcr:repository:init

That’s it, the bundle is configured properly now!