Symfony 4 Sortable Doctrine Extension

This is how to configure this extension on Symfony 4. First you need to install the bundle following the instruccions here: https://symfony.com/doc/master/bundles/StofDoctrineExtensionsBundle/installation.html#installation-using-symfony-flex

Configure the bundle in config/packages/stof_doctrine_extensions.yaml (create the file if it doesn’t exist) like this:

stof_doctrine_extensions:
    default_locale: eu_ES
    orm:
        default:
            sortable: true

Edit your entity and add a new column

/**
 * @Gedmo\SortablePosition
 * @ORM\Column(name="position", type="integer")
 */
private $position;

Don’t forget to add the use Gedmo\Mapping\Annotation as Gedmo;

And here comes the main change you have to do in symfony4, you need to use SortableRepository. If you aren’t using a repository for your entity you can add this line on your entity:

/**
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
 */

But if you are using a repository with your custom querys, you need to make 2 changes:

Change ServiceEntityRepository to SortableRepository and import use Gedmo\Sortable\Entity\Repository\SortableRepository; class:

  class EmployeeRepository extends SortableRepository

Change the construct function to:

  public function __construct(EntityManagerInterface $em)
  {
      parent::__construct($em, $em->getClassMetadata(Employee::class));
  }