How to fix “This repository can be attached only to ORM sortable listener” error in Gedmo sortable?

空扰寡人 提交于 2021-01-27 05:24:17

问题


When usieing StofDoctrineExtensions (which is a Symfony2 port of Gedmo Doctrine Extensions) Sortable behaviour I kept on getting this error:

This repository can be attached only to ORM sortable listener

Since I could not easily find the answer in official docs I'm leaving an answer here for future reference.


回答1:


You need to enable any listeners you are using. In this case, Sortable.

stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            sortable: true

For Symfony 4, add this configuration in /config/packages/stof_doctrine_extensions.yaml. For older versions of Symfony, add it to config.yml.




回答2:


In order to use Sortable behaviour you need to add an event listener to your bundle's boot method

<?php

namespace Acme\DemoBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeDemoBundle extends Bundle
{
    public function boot()
    {
        // get the doctrine 2 entity manager
        $em = $this->container->get('doctrine.orm.default_entity_manager');

        // get the event manager
        $evm = $em->getEventManager();
        $evm->addEventSubscriber(new \Gedmo\Sortable\SortableListener);
    }
}


来源:https://stackoverflow.com/questions/14414243/how-to-fix-this-repository-can-be-attached-only-to-orm-sortable-listener-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!