问题
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?
回答1:
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.
回答2:
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 $baseRoutePattern = 'page-group';
回答3:
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.
来源:https://stackoverflow.com/questions/12828072/sonataadminbundle-multiple-admin-section-for-same-entity