Version

Documentation » Classification

3. Classification Types »

« 1. Introduction

2. Installation

2.1. Prerequisites

PHP 7.1 and Symfony >=3.4 or >= 4.2 are needed to make this bundle work, there are also some Sonata dependencies that need to be installed and configured beforehand:

Add SonataClassificationBundle via composer:

$ composer require sonata-project/classification-bundle

Now, add the new SonataClassificationBundle to bundles.php file:

<?php

// config/bundles.php

return [
    //...
    Sonata\ClassificationBundle\SonataClassificationBundle::class => ['all' => true],
];

Note

If you are not using Symfony Flex, you should enable bundles in your AppKernel.php.

<?php

// app/AppKernel.php

public function registerBundles()
{
    return [
        new Sonata\ClassificationBundle\SonataClassificationBundle(),
        // ...
    ];
}

2.2. Configuration

2.2.1. Doctrine Configuration

Add these bundles in the config mapping definition (or enable auto_mapping):

# config/packages/doctrine.yaml

doctrine:
    orm:
        entity_managers:
            default:
                mappings:
                    ApplicationSonataClassificationBundle: ~
                    SonataClassificationBundle: ~

Note

If you are not using Symfony Flex, this configuration should be added to app/config/config.yml.

2.3. Extending the Bundle

At this point, the bundle is functional, but not quite ready yet. You need to generate the correct entities for the media:

bin/console sonata:easy-extends:generate SonataClassificationBundle --dest=src --namespace_prefix=App

Note

If you are not using Symfony Flex, use command without --namespace_prefix=App.

With provided parameters, the files are generated in src/Application/Sonata/ClassificationBundle.

Note

The command will generate domain objects in App\Application namespace. So you can point entities’ associations to a global and common namespace. This will make Entities sharing easier as your models will allow to point to a global namespace. For instance the tag will be App\Application\Sonata\ClassificationBundle\Entity\Tag.

Note

If you are not using Symfony Flex, the namespace will be Application\Sonata\ClassificationBundle\Entity.

Now, add the new Application Bundle into the bundles.php:

<?php

// config/bundles.php

return [
    //...
    App\Application\Sonata\ClassificationBundle\ApplicationSonataClassificationBundle::class => ['all' => true],
];

Note

If you are not using Symfony Flex, add the new Application Bundle into your AppKernel.php.

<?php

// app/AppKernel.php

class AppKernel {

    public function registerBundles()
    {
        return [
            // Application Bundles
            // ...
            new Application\Sonata\ClassificationBundle\ApplicationSonataClassificationBundle(),
            // ...
        ];
    }
}

And configure ClassificationBundle to use the newly generated classes:

# config/packages/sonata.yaml

sonata_classification:
    class:
        tag: App\Application\Sonata\ClassificationBundle\Entity\Tag
        category: App\Application\Sonata\ClassificationBundle\Entity\Category
        collection: App\Application\Sonata\ClassificationBundle\Entity\Collection
        context: App\Application\Sonata\ClassificationBundle\Entity\Context

Note

If you are not using Symfony Flex, add classes without the App\ part and this configuration should be added to app/config/config.yml

The only thing left is to update your schema:

bin/console doctrine:schema:update --force

Found a typo? Something is wrong in this documentation? Just fork and edit it!