Reputation: 197
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.
Upvotes: 1
Views: 673
Reputation: 8374
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:
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();
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());
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'];
Upvotes: 1