Can I populate a Sonata entity export using only getters?

  doctrine, php, sonata, sonata-admin, symfony

I am building a very simple billing system for a media company using Sonata that includes Payment entities for paying Reporters. The only purpose of a Payment is to be exported to a spreadsheet. We will never edit Payments directly.

I have a bunch of special getters in my Payment entity that don’t correspond directly to properties of that entity. Stuff like this:

public function getReporterName():string
{
    return
        $this->reporter->getUser()->getFirstname() .
        ' ' .
        $this->reporter->getUser()->getLastname()
        ;
}

In my Sonata admin class for this entity, I have told my list view to show reporterName as if it is a property. Sonata is smart enough to find and use the getter to retrieve the string I’m looking for. No problem.

The issue comes when I add that pseudo field name into the configureDatagridFilters() method like so:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('id')
        ->add('paymentComplete')
        ->add('reporterName')
    ;
}

Now I get a nasty message when trying to export a spreadsheet, saying this:

No metadata found for property
AppBundleEntityPayment::$reporterName. Please make sure your
Doctrine mapping is properly configured.

Is there a way to do what I’m trying to do here? Or will I simply have to suck it up and duplicate some data in the database in order to make my export work?

If it helps, I’ll reiterate here that we only need this entity for the purpose of exporting to a spreadsheet. It will never be edited manually.

Source: Symfony Questions

LEAVE A COMMENT