简体   繁体   English

我可以仅使用 getter 填充 Sonata 实体导出吗?

[英]Can I populate a Sonata entity export using only getters?

I am building a very simple billing system for a media company using Sonata that includes Payment entities for paying Reporters.我正在为一家使用 Sonata 的媒体公司构建一个非常简单的计费系统,其中包括用于支付记者的支付实体。 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.在我的奏鸣曲管理员 class 中,我已经告诉我的列表视图显示reporterName就好像它是一个属性。 Sonata is smart enough to find and use the getter to retrieve the string I'm looking for. Sonata 足够聪明,可以找到并使用 getter 来检索我正在寻找的字符串。 No problem.没问题。

The issue comes when I add that pseudo field name into the configureDatagridFilters() method like so:当我将该伪字段名称添加到 configureDatagridFilters() 方法时,问题就出现了,如下所示:

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 AppBundle\Entity\Payment::$reporterName .找不到属性AppBundle\Entity\Payment::$reporterName的元数据。 Please make sure your Doctrine mapping is properly configured.请确保您的 Doctrine 映射配置正确。

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.它永远不会被手动编辑。

I think you are stuck using two fields (unless you add a mapped field for the full name).我认为您被困在使用两个字段(除非您为名添加映射字段)。
You can use the dot syntax to filter by sub entity properties :您可以使用点语法按子实体属性进行过滤

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('id')
        ->add('paymentComplete')
        ->add('reporter.user.firstname', null, ['label' => 'Reporter Firstname'])
        ->add('reporter.user.lastname', null, ['label' => 'Reporter Lastname'])
    ;
}

I ended up working with the dedicated export method to solve this.我最终使用专用的导出方法来解决这个问题。

https://symfony.com/doc/current/bundles/SonataAdminBundle/reference/action_export.html https://symfony.com/doc/current/bundles/SonataAdminBundle/reference/action_export.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM