Skip to content
Advertisement

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

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

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

    /**
     * @ORMManyToOne(targetEntity=Department::class, inversedBy="hikes")
     * @ORMJoinColumn(nullable=false)
     * @AssertNotBlank(message="libdepartmentRequired")
     */
    private $department;
    
    // ...
}
<?php
class Department
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

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

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

    /**
     * @ORMOneToMany(targetEntity="AppEntityHike", 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 ?

Advertisement

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement