Sonataadminbundle: Multiple admin section for same entity
Asked Answered
I

4

24

I have entity class Page with column type=integer. When I do:

   <service id="sonata.admin.pages" class="Main\ProgramBundle\Admin\PageAdmin">
      <tag name="sonata.admin" manager_type="orm" group="dashboard" label="Pages"/>
      <argument />
      <argument>Main\ProgramBundle\Entity\Page</argument>
      <argument>SonataAdminBundle:CRUD</argument>
  </service>


   <service id="sonata.admin.groups" class="Main\ProgramBundle\Admin\GroupAdmin">
      <tag name="sonata.admin" manager_type="orm" group="stories" label="Groups"/>
      <argument />
      <argument>Main\ProgramBundle\Entity\Page</argument>
      <argument>SonataAdminBundle:CRUD</argument>
  </service>

In short, both sections work on same entity except that each have different queries and forms.

But what happens is that sonata always executes Admin/GroupAdmin, even if I select PageAdmin. How to do this?

Interact answered 10/10, 2012 at 20:47 Comment(0)
R
42

I don't have enough reputation to add a comment to the previous answer, but it is missing the following information:

You also need to define a unique $baseRouteName value in addition to $baseRoutePattern in your admin classes:

protected $baseRouteName = 'admin_vendor_bundlename_adminclassname';

protected $baseRoutePattern = 'unique-route-pattern';

You only need to do this to one class, but consider doing it in both to keep it clear what's going on.

Reliance answered 12/11, 2012 at 10:31 Comment(2)
You would still run into problems when you add this entity to another admin. Sonata will trigger an error telling that there are to many admins registered.. This is fixed by adding 'admin_code' => 'sonata.product.admin.product' to the add function. This will tell sonata which admin to use specifically.Yim
Thanks! @Yim I had the problem in a show just added ->add('someProperty',null, array('admin_code' => 'some.service.admin,id'))Siloxane
D
5

Sonata creates routes automatically based on your entity names. So if you have 2 admin classes, there is a conflict. You have to configure different route pattern.

Add this property to Main\ProgramBundle\Admin\GroupAdmin.php:

protected $baseRouteName = 'page_group';
protected $baseRoutePattern = 'page-group';
Despotic answered 21/10, 2012 at 9:45 Comment(4)
I did, now both url's are admin/page-group/list, even if I place these properties (different, ofcourse) in both PageAdmin and GroupAdmin :(Interact
Don't put this property to both classes. You should put this property only to one of your Admin classes. If you put it to both, you will go to same conflict situation as before.Despotic
The first thing I tried is exactly as you told; put the property only in one admin class. That didn't work and only after that, I tried both cases.Interact
protected $baseRouteName is also required in order to work properlyPassus
S
1

I don't know how it was in 2012, but in 2022 it's all in the official documentation, on the Routing page.

One can override generateBaseRouteName and generateBaseRoutePattern methods in each Admin in order to control which name (to be used in e.g. <a href="{{ path('admin_app_post_list') }}">Post List</a>) and URL a route has.

// src/Admin/PostAdmin.php
final class PostAdmin extends AbstractAdmin
{
    protected function generateBaseRouteName(bool $isChildAdmin = false): string
    {
        return 'sonata_post';
        // will result in routes named:
        //   sonata_post_list
        //   sonata_post_create
        //   etc
    }

    protected function generateBaseRoutePattern(bool $isChildAdmin = false): string
    {
        return 'Post';
    }
}

Barry's answer still seems to work but maybe it has unwanted side-effects, I don't know.

Selfhelp answered 12/8, 2022 at 8:39 Comment(0)
K
0

You certainly found your solution, but another way to do this would be to create a parent class and inherit using inheritance mapping. You are then able to have different repository and sonata admin would not work differently.

Katusha answered 20/6, 2013 at 21:15 Comment(1)
I tried it first up with an entity extending my original one - but console doctrine:schema:update fails as you are trying to create the same table twice - or worse, you end up with two tables in the DB.Featured

© 2022 - 2024 — McMap. All rights reserved.