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?

Zeljko
  • 5,048
  • 5
  • 36
  • 46

4 Answers4

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.

Barry
  • 746
  • 9
  • 20
  • 4
    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. – 11mb May 05 '15 at 20:19
  • 1
    Thanks! @11mb I had the problem in a show just added ```->add('someProperty',null, array('admin_code' => 'some.service.admin,id'))``` – Cassiano Jan 28 '16 at 17:48
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';
pulzarraider
  • 2,297
  • 19
  • 26
  • 1
    I did, now both url's are `admin/page-group/list`, even if I place these properties (different, ofcourse) in both PageAdmin and GroupAdmin :( – Zeljko Oct 22 '12 at 21:07
  • 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. – pulzarraider Oct 25 '12 at 18:18
  • 3
    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. – Zeljko Nov 02 '12 at 18:56
  • protected $baseRouteName is also required in order to work properly – Srdjan Sep 02 '20 at 13:30
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.

learner
  • 341
  • 2
  • 11
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.

bodrak
  • 46
  • 3
  • 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. – Alister Bulman Jul 10 '14 at 15:10