PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

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

 May 12, 2022     easyadmin, symfony     No comments   

Issue

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.



Answered By - Kamil Adryjanek
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing