easyadmin 3 - Sorting by linked entity’s property instead of id

2.4k views Asked by At

I have one entity Hike which have relation with another named Department

<?php
class Hike
{
    private $id;
    private $name;
    private $description;

    /**
     * @ORM\ManyToOne(targetEntity=Department::class, inversedBy="hikes")
     * @ORM\JoinColumn(nullable=false)
     * @Assert\NotBlank(message="libdepartmentRequired")
     */
    private $department;
    
    // ...
}
<?php
class Department
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=3)
     */
    private $department_code;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $department_nom;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Hike", mappedBy="department")
     */
    private $hikes;
    
    // ...
}

In easyAdmin3 rendering is fine like enter image description here

But when I sorting by department I see that easyAdmin3 sort by department's id, and I would like sorting by department_nom

I saw many solutions but they all using easyAdmin2 and easy_admin.yaml which no longer exists now.

There is a way to achieve that ?

1

There are 1 answers

0
Cephalopodius On BEST ANSWER

Use configureCrud. Something like this should do the trick.

public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setDefaultSort(['departement' => 'DESC'])

        ;
    }

You can also use the filter to get the resultat wanted.