Create a Bundle

New bundle can be created either manually, or automatically using standard Symfony console command.

Create a Bundle Manually

First you need to specify name and namespace of your bundle. Symfony framework already has best practices for bundle structure and bundle name and we recommend to follow these practices and use them.

Let us assume that we want to create the AcmeNewBundle and put it under the namespace Acme\Bundle\NewBundle in the /src directory. We need to create the corresponding directory structure and the bundle file with the following content:

1
2
3
4
5
6
7
8
9
<?php
// src/Acme/Bundle/NewBundle/AcmeNewBundle.php
namespace Acme\Bundle\NewBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeNewBundle extends Bundle
{
}

Basically, it is a regular Symfony bundle. The only difference is in the way it will be enabled (see chapter Enable a Bundle).

Enable a Bundle

Now you have all the required files to enable the new bundle. To enable the bundle:

  1. Create Resources/config/oro/bundles.yml with the following content:

    1
    2
    3
    # src/Acme/Bundle/NewBundle/Resources/config/oro/bundles.yml
    bundles:
        - Acme\Bundle\NewBundle\AcmeNewBundle
    

    This file provides a list of bundles to register — all such files will be automatically parsed to load required bundles.

  2. Regenerate the application cache using the console command cache:clear:

    1
    2
    user@host:/var/www/vhosts/platform-application$ php bin/console cache:clear
    Clearing the cache for the dev environment with debug true
    

    Note

    If you are working in production environment, you have to use parameter --env=prod with the command.

Now open the application user interface in development mode (use the link http://<oro-application-base-url>/app_dev.php/) and click the Symfony profiler config icon:

../../../_images/dashboard.png

Here you can find your new bundle in the list of active bundles:

../../../_images/profiler.png

That is all — your bundle is registered and active!

Generate an Installer for a Bundle

When you have implemented new entities, you want to be sure that upon installing the application, the entities are added to the database. For this, you need to create an installer. You can do it manually, however, it is more convenient to use a dump of the database as a template.

To create an installer for AcmeBundle:

  1. Clear the application cache:

    1
    bin/console cache:clear
    
  2. Apply the changes that you defined in your code to the database:

    1
    bin/console doctrine:schema:update
    
  3. Generate an installer and save it to the AcmeBundleInstaller.php:

    1
    bin/console oro:migration:dump --bundle=AcmeBundle > AcmeBundleInstaller.php
    

    Hint

    The generated installer may contain a lot of excessive information as the same database table might contain options related to different bundles and entities while the generator has no option to distinguish which entity ‘has added’ particular options. Delete the information unrelated to your entities from the output file.

Move AcmeBundleInstall.php to the AcmeBundle/Migrations/Schema directory.
  1. Reinstall your application instance.

  2. Check that the database is synced with your code:

    1
    bin/console doctrine:schema:update --dump-sql
    

    If the database is successfully synchronized, you will see the following message:

    1
    Nothing to update - your database is already in sync with the current entity metadata.