Sonata Admin Bundle: show total count of collection on list view

前端 未结 5 938
孤城傲影
孤城傲影 2021-01-06 04:50

Is there any way to show total count of collection on list view? Imagine that there is a user that can have many links. How can I show total links count on list view?

5条回答
  •  走了就别回头了
    2021-01-06 05:13

    My answer is similar to Khalid (above) but has some key differences.

    If you wrap the collection in a count( $entity->getLinks() ) then this will issue a query which returns every link association.

    The downside of this is that if you have 1000s of Links associated, the memory resources required will need to be sufficient for hydrate each entity. (Which can be huge if you have thousands of different entities).

    Instead, you should mark your Entities as EXTRA_LAZY and then use the --$entity->getLinks()->count()` method which will not do any hydration, instead it will only issue the COUNT queries.

    http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html

    So do the following:

       /**
         * @ManyToMany(targetEntity="Links", mappedBy="whatever", fetch="EXTRA_LAZY")
         */
        public $links;
    

    Then you can call:

    public function getTotalLinks(){
            return $this->getLinks()->count();
        }
    

    And it will be super quick.

提交回复
热议问题