Search code examples
symfonyeasyadmin

How to display OneToMany relationship in list/search view EasyAdminBundle 2x?


I use EasyAdminBundle 2x and Symfony 4.4. I have two entities:

UserCase entity:

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\MyReports", mappedBy="userCase")
     */
    protected $myReports;

And MyReports entity:

   /**
     * @ORM\ManyToOne(targetEntity="App\ORM\Entity\UserCase", inversedBy="myReports", cascade={"persist"})
     * @ORM\JoinColumn(name="user_case", referencedColumnName="id", onDelete="CASCADE")
     * @Serializer\Exclude()
     */
    protected $userCase;

    /**
     * @var DateTime
     *
     * @ORM\Column(name="verified_date", type="datetime", nullable = true)
     */
    protected $verifiedDate;

In the list view of UserCase, I would like to show the verifiedDate of the latest MyReports that UserCase relation with. Something like that:

easy_admin:
    entities:
        UserCase:
            class: App\ORM\Entity\UserCase
            list:
                fields:
                    - { property: 'myReports.last.verifiedDate'}
                    

How could I do it?


Solution

  • Please useEasyAdmin Virtual Properties:

    • add verifiedDate property to easy_admin config file (instead of 'myReports.last.verifiedDate');
    • add new public method to UseCase entity: getVerifiedDate with your logic of selecting last record with proper date;

    Also: I'd recommend adding extra property to UserCase e.g. verifiedDate that will be updated instead of fetching last record for each row in grid.