1

I'm using SonataAdmin and FosUserBundle with Symfony 4. I want to use the export feature to export whole users' data in CSV, JSON ... When a trigger the export, the roles column in the file is empty or null.

In the UserAdmin class, I have overridden the getExportFields function with the call of a specific method to get the role as explained in this post. Sonata admin export fields with collection fields But it doesn't work.

Example in my case:

 public function getExportFields()
 {
    return [
        'id',
        'username',
        'roles' => 'rolesExported'
    ];
 }

And in my User Entity:

public function getRolesExported()
{
    $exportedRoles = [];
    foreach ($this->getRealRoles() as $role) {

        $exportedRoles[] = $role->__toString();
    }

    return $this->rolesExported = implode(' - ', $exportedRoles);
}

In this case, when I trigger the export, my web browser shows the error

'website is inaccessible' with no error in the dev.log.

When I delete 'roles' => 'rolesExported' in the getExportFields function, the export is well triggered.

  • SonataAdmin version : 3.35
  • FosUserBundle version : 2.1.2
  • Symfony version: 4.3.2 (I know that I need to update it)
Dhia Djobbi
  • 1,176
  • 2
  • 15
  • 35
chaillouvincent
  • 197
  • 1
  • 3
  • 17
  • you did something, where you don't want to show the code for, you use a different symfony version, that post is 2 years old, there could be a number of reasons why it doesn't work anymore. Please show some code, and shortly elaborate what "it doesn't work" precisely means! (could mean anything from "errors" to "wrong output" to "no effect at all") – Jakumi Jul 02 '20 at 07:31
  • I have added some details to understand the issue. Hope it will help. – chaillouvincent Jul 02 '20 at 07:48
  • as a side note: you should really find out, where the errors are put. are you running this on a proper web server? are you running this on dev environment? on a web server, the php log might be in [symfony]/var/log/(prod/dev).log, or /var/log/php_errors(.log) or /var/log/php-fpm/errors.log or some other general log file. – Jakumi Jul 02 '20 at 08:15

1 Answers1

1

I suspect that the __toString() call causes a problem.

although the post you used as your inspiration explicitly says that it wants to export collections, I assume you might want to export an array.

Since I don't know the type of your $role objects, for debugging purposes first replace $role->__toString() with gettype($role), so the line is: $exportedRoles[] = gettype($role);

I see three cases here:

  1. object or for multiple roles object - object - ..., in that case, you should select a method of Role that returns a proper string or create one at that place, like $exportedRoles[] = $role->getName();
  2. string or for multiple roles string - string - ..., in that case your "real" roles is just an array, and you can replace the contents of your function with return implode(' - ', $this->getRealRoles());
  3. array or for multiple roles array - array - ..., in that case you have an array for each role, and those don't provide __toString. Select a method to construct the exported role, like $exportedRoles[] = $role['name'];
Jakumi
  • 8,043
  • 2
  • 15
  • 32