Search code examples
symfonysonata-admin

SonataAdminBundle - multiple list views of one entity


Let's just say i have entity named Offer.

I want to make multiple list views for Offer entity. Each list view should contain offers with different states (i.e. draft, active, inactive...).

So far I created two offer admins: DraftOfferAdmin and ActiveOfferAdmin. Here I defined custom queries:

public function createQuery($context = 'list')
{
    /** @var ModelManager $modelManager */
    $modelManager = $this->getModelManager();
    $entityManager = $modelManager->getEntityManager($this->getClass());

    $queryBuilder = $entityManager->createQueryBuilder();
    $queryBuilder
        ->select('o')
        ->from($this->getClass(), 'o')
        ->where('o.state = :state')
        ->setParameter('state', 'draft');

    $query = new ProxyQuery($queryBuilder);

    foreach ($this->extensions as $extension) {
        $extension->configureQuery($this, $query, $context);
    }

    return $query;
}

Query seems to be working fine!

I defined Admins in services:

services:
    admin.draft_offer:
        class: IndexBundle\Admin\Offer\DraftOfferAdmin
        arguments:
            - null
            - IndexBundle\Entity\Offer
            - IndexBundle:CRUD
        tags:
            - { name: sonata.admin, manager_type: orm, group: Offers, label: Draft Offers }
    admin.unverified_offer:
        class: IndexBundle\Admin\Offer\UnverifiedOfferAdmin
        arguments:
            - null
            - IndexBundle\Entity\Offer
            - IndexBundle:CRUD
        tags:
            - { name: sonata.admin, manager_type: orm, group: Offers, label: Unverified Offers }

But both list view pages share the same URL http://domain.com/admin/index/offer/list. Any ideas what do I miss in my configurations?


Solution

  • This happens probably because while the admin classes are different, your entity class is the same. I`d recommend this article in order to achieve the functionality you require, more user friendly too.

    Now that I need exactly this functionality in one project :

    In your admin class you need to set the route and the route pattern like

    class ClassAdmin extends AbstractAdmin
    {
        protected $baseRoutePattern = 'class-route';
        protected $baseRouteName = 'class-route';
    
      public function createQuery($context = 'list')
      {
        $query = parent::createQuery($context);
        $query->join($query->getRootAlias() . '.status', 'st');
        $query->andWhere('st.id = :status')
            ->setParameter('status', $statis);
        return $query;
       }
       //admin class code.. 
    }
    

    and include it the standart way..