phpsymfonysonata-admin

SONATA MY ADMIN : TypeError: trim() expects parameter 1 to be string, object given


I am currently working on a website and I have to use Sonata Admin for the back office. I am upgrading the project from symfony 3 to symfony 4 and I have a problem when I try to filter the instantiated entities.

I configured a filter that way :

protected function configureDatagridFilters(DatagridMapper $datagridMapper) {
   $datagridMapper->add('name',null , [], EntityType::class, array(
        'class' => Universe::class,
        'choice_label' => 'name',
    ));
}

It references the existing Universe entities nicely in the dropdown list (showing their name like I asked). See for yourself :

dropdown filter

But when i try to actually filter with name from that list I get that error :

TypeError: trim() expects parameter 1 to be string, object given

I looked on the internet and from what I found on the documentation I configured the filters exactly like they did (source : https://symfony.com/doc/current/bundles/SonataAdminBundle/getting_started/the_list_view.html#filtering-by-category).

Do you have any idea what did I do wrong ? I don't know what to give more than that to find the error...

Thanks a lot !

EDIT

What I don't understant either, is that if the property I want to filter by is a foreign key it works like a charm. For example here jobid is a foreign key to a Job entity while we are under another entity.

$datagridMapper->add('jobId', null, array('label' => 'Section'), EntityType::class, array(
    'class'    => 'App\Entity\Admin\Job',
    'choice_label' => 'name',
));

With that in mind I can't understand why it doesn't work.

2ND EDIT

Stack trace :

TypeError:
trim() expects parameter 1 to be string, object given

  at vendor/sonata-project/doctrine-orm-admin-bundle/src/Filter/StringFilter.php:33
  at trim(object(Collection))
     (vendor/sonata-project/doctrine-orm-admin-bundle/src/Filter/StringFilter.php:33)
  at Sonata\DoctrineORMAdminBundle\Filter\StringFilter->filter(object(ProxyQuery), 'o', 'name', array('type' => null, 'value' => object(Collection)))
     (vendor/sonata-project/doctrine-orm-admin-bundle/src/Filter/Filter.php:33)
  at Sonata\DoctrineORMAdminBundle\Filter\Filter->apply(object(ProxyQuery), array('type' => null, 'value' => object(Collection)))
     (vendor/sonata-project/admin-bundle/src/Datagrid/Datagrid.php:155)
  at Sonata\AdminBundle\Datagrid\Datagrid->buildPager()
     (vendor/sonata-project/admin-bundle/src/Datagrid/Datagrid.php:292)
  at Sonata\AdminBundle\Datagrid\Datagrid->getForm()
     (vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php:135)
  at Sonata\AdminBundle\Controller\CRUDController->listAction()
     (vendor/symfony/http-kernel/HttpKernel.php:151)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:68)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:198)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:25)

LAST EDIT

Just in case for those who will search something like that in the future.

I ended doing something like this :

$datagridMapper->add('name','doctrine_orm_string' , array(), ChoiceType::class,
     array('choices' => $this->getAllByFieldService->fetchAllDistinctBy(Action::class, 'name')));

With fetchAllDistinctBy a method of my own creating the list of choices I need.


Solution

  • If I understand the problem correct, you want to filter the entities based on the field of the same entity?

    namespace App\Admin;
    
    use App\Entity\Category;
    use Sonata\AdminBundle\Datagrid\DatagridMapper;
    use Symfony\Bridge\Doctrine\Form\Type\EntityType;
    
    final class UniverseAdmin extends AbstractAdmin
    {
        protected function configureDatagridFilters(DatagridMapper $datagridMapper)
        {
            $datagridMapper
                ->add('title')
                ->add('name',null , [], EntityType::class, [
                    'class' => Universe::class,
                    'choice_label' => 'name',
                ])
            ;
        }
    }
    

    I'm afraid this won't work. You should create an entity which contains the ‘name’ field and create relations between the name entity and the proper entity.