Sonataadminbundle: Search block admin - "The Request object has not been set"

Created on 28 Nov 2014  路  12Comments  路  Source: sonata-project/SonataAdminBundle

Almost the same issue like here: http://stackoverflow.com/questions/19551715/sonata-admin-search-feature

When I try to search it throws:

An exception has been thrown during the rendering of a template ("The Request object has not been set") in SonataAdminBundle:Core:search.html.twig at line 48. 

Tried to solve like in Stackoverflow but didn't work.

Most helpful comment

If you have configured a base admin for your application which inherits sonata's Admin then in your admin you can easily override getRequest() method and provide the request object from main container to by pass this error

    use Sonata\AdminBundle\Admin\Admin;

    class YourBaseAdmin extends Admin
    {
        /**
         * {@inheritdoc}
         */
        public function getRequest()
        {
            if (!$this->request) {
                //  throw new \RuntimeException('The Request object has not been set');
                $this->request = $this->getConfigurationPool()->getContainer()->get('Request');
            }

            return $this->request;
        }

    }

All 12 comments

If you have configured a base admin for your application which inherits sonata's Admin then in your admin you can easily override getRequest() method and provide the request object from main container to by pass this error

    use Sonata\AdminBundle\Admin\Admin;

    class YourBaseAdmin extends Admin
    {
        /**
         * {@inheritdoc}
         */
        public function getRequest()
        {
            if (!$this->request) {
                //  throw new \RuntimeException('The Request object has not been set');
                $this->request = $this->getConfigurationPool()->getContainer()->get('Request');
            }

            return $this->request;
        }

    }

got same now on my project, work before, don't know why it doesn't now

i don't use $request in any of my admin class, i had disabled sonata.admin.block.admin_list in the blocks

sonata admin 2.3

Please reopen if you still experience this bug with Sonata 3.

This is still a problem in Sonata 3

    /**
     * {@inheritdoc}
     */
    public function getRequest()
    {
        if (!$this->request) {
            // throw new \RuntimeException('The Request object has not been set');

            return $this->request = $this->getConfigurationPool()->getContainer()->get('request_stack')->getCurrentRequest();
        }

        return $this->request;
    }


This worked when i checked it in AbstractAdmin (Symfony 3).

Are we expecting any fix for this.

The workaround posted by @MKhalidJunaid made it work. It should have worked without the additional code in the Admin, right?

@sujayjaju
Thanks man.

But what i'm looking for is straight fix from core itself rather than work arounds. I can go with Trait for it do so but that would just add few more extra lines of code.

This was connected to Translations. TranslatableExtension in SonataTranslations had a call getRequest() that throws an exception, this is fixed with https://github.com/sonata-project/SonataTranslationBundle/pull/185. If someone still encounters this with search we will reopen.

I am still experiencing this with Translation Bundle 2.2 (latest update).

Can you try with 2.x branch? As new version is still not released

I am now receiving this error:

An exception has been thrown during the rendering of a template ("[Semantical Error] line 0, col 94 near 'tff WHERE ((tff.field': Error: Class AppBundle\Entity\CardMatch has no association named translations").

My entity is an AbstractTranslatable and has no personal translation table, therefore it doesn't have a translations property.

Can you open a new issue on https://github.com/sonata-project/SonataTranslationBundle repo as this is no longer connected to this one.

Was this page helpful?
0 / 5 - 0 ratings