SonataAdmin sorting on collections, on translatable entities

12 Views Asked by At

Im trying to add some sorting to my admin column with sonataAdmin:

My entity Country ;

 #[ORM\OneToMany(
        mappedBy: 'entity',
        targetEntity: CountryTranslation::class,
        cascade: ['all'],
        orphanRemoval: true,
    )]
    private Collection $translations;

My CountryTranslation Entity :

 #[Assert\NotNull]
    #[ORM\Column(length: 255, nullable: true)]
    private ?string $label = null;

    #[ORM\ManyToOne(inversedBy: 'translations')]
    #[ORM\JoinColumn(nullable: false)]
    private Country $entity;

my ConfigureListFields :

protected function configureListFields(ListMapper $list): void
    {
        $locale = $this->getRequest()->getLocale();
        $list
            ->addIdentifier('code', null, ['label' => 'admin.entity_translatable.label.code'])
            ->add('label', FieldDescriptionInterface::TYPE_STRING, [
                'label' => 'admin.entity_translatable.label.label',
                'accessor' => function ($subject) {
                    return $subject->getTranslation($this->getRequest()->getLocale());
                },
                'sortable' => true,
                'sort_field_mapping' => [
                    'fieldname' => 'translations' // should be fieldName
                    ],
                'sort_parent_association_mappings' => [],
            ])
            ->add('createdAt', null, ['label' => 'admin.entity_translatable.label.created_at'])
            ->add('createdBy', null, ['label' => 'admin.entity_translatable.label.created_by'])
            ->add('updatedAt', null, ['label' => 'admin.entity_translatable.label.updated_at'])
            ->add('updatedBy', null, ['label' => 'admin.entity_translatable.label.updated_by']);
    }
}

Can't figure out how to make this sort properly

Tryed multiple thing

0

There are 0 best solutions below