Sonataadminbundle: Multiple admin class with children for same entity

Created on 11 Sep 2017  路  9Comments  路  Source: sonata-project/SonataAdminBundle

I have 2 admin classes for the same entity. The classes have the same children admin class.
The 'admin_code' argument resolve the problem " Unable to find a valid admin " for the main admin class.
But on child admin page the error message remain.

With logistique admin page and logistique2 Admin page for logistique entity:
/app_dev.php/admin/logistique/32/edit : OK
/app_dev.php/admin/logistique/32/ligne/create : Get error message "Unable to find a valid admin for the class MagasinBundle\Entity\Logistique".

Need to do 2 child admin too ?

The child admin class have this fonction for Parent declaration :

    public function getParentAssociationMapping()
    {
        $em = $this->modelManager->getEntityManager('MagasinBundle\Entity\Logistique'); 
        $className = $em->getClassMetadata(get_class($this->getParent()->getObject($this->getParent()->getRequest()->get('id'))))->getTableName();

        return strtolower( $className );
    }


Stack Trace

[1] RuntimeException: Unable to find a valid admin for the class: MagasinBundle\Entity\Logistique, there are too many registered: magasin.admin.logistique, magasin.admin.logistique2 at n/a in C:\xampp\htdocs\stocks\vendor\sonata-project\admin-bundle\Admin\Pool.php line 195 at Sonata\AdminBundle\Admin\Pool->getAdminByClass('MagasinBundle\Entity\Logistique') in C:\xampp\htdocs\stocks\vendor\sonata-project\admin-bundle\Admin\AbstractAdmin.php line 1313 at Sonata\AdminBundle\Admin\AbstractAdmin->attachAdminClass(object(FieldDescription)) in C:\xampp\htdocs\stocks\vendor\sonata-project\doctrine-orm-admin-bundle\Builder\FormContractor.php line 75 at Sonata\DoctrineORMAdminBundle\Builder\FormContractor->fixFieldDescription(object(LigneAdmin), object(FieldDescription), array('type' => 'Symfony\Bridge\Doctrine\Form\Type\EntityType')) in C:\xampp\htdocs\stocks\vendor\sonata-project\admin-bundle\Form\FormMapper.php line 111 at Sonata\AdminBundle\Form\FormMapper->add('logistique', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', array('class' => 'MagasinBundle:Ligne', 'label' => 'Requ锚te Logistique', 'required' => true, 'disabled' => true)) in C:\xampp\htdocs\stocks\src\MagasinBundle\Admin\LigneAdmin.php line 130 at MagasinBundle\Admin\LigneAdmin->configureFormFields(object(FormMapper)) in C:\xampp\htdocs\stocks\vendor\sonata-project\admin-bundle\Admin\AbstractAdmin.php line 1292 at Sonata\AdminBundle\Admin\AbstractAdmin->defineFormBuilder(object(FormBuilder)) in C:\xampp\htdocs\stocks\vendor\sonata-project\admin-bundle\Admin\AbstractAdmin.php line 1277 at Sonata\AdminBundle\Admin\AbstractAdmin->getFormBuilder() in C:\xampp\htdocs\stocks\vendor\sonata-project\admin-bundle\Admin\AbstractAdmin.php line 3236 at Sonata\AdminBundle\Admin\AbstractAdmin->buildForm() in C:\xampp\htdocs\stocks\vendor\sonata-project\admin-bundle\Admin\AbstractAdmin.php line 1345 at Sonata\AdminBundle\Admin\AbstractAdmin->getForm() in C:\xampp\htdocs\stocks\vendor\sonata-project\admin-bundle\Controller\CRUDController.php line 502 at Sonata\AdminBundle\Controller\CRUDController->createAction() in line at call_user_func_array(array(object(CRUDController), 'createAction'), array()) in C:\xampp\htdocs\stocks\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php line 144 at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1') in C:\xampp\htdocs\stocks\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php line 64 at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true) in C:\xampp\htdocs\stocks\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel.php line 69 at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true) in C:\xampp\htdocs\stocks\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php line 185 at Symfony\Component\HttpKernel\Kernel->handle(object(Request)) in C:\xampp\htdocs\stocks\web\app_dev.php line 20

All 9 comments

Each entity class should have 1 and 1 Admin Class only.

@binhle410 Multiple admin is possible : https://stackoverflow.com/questions/12828072/sonataadminbundle-multiple-admin-section-for-same-entity.
But its bugging when i access to child admin.

And indeed:

<?php
->add('logistique', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', array(
    'class' => 'MagasinBundle:Ligne', 
    'label' => 'Requ锚te Logistique', 
    'required' => true,
    'disabled' => true
))

The 'admin_code' argument resolve the problem " Unable to find a valid admin " for the main admin class.

What do you mean by this? Apparently, you did not specify one, did you?

This ->add('logistique' ...) come from child Admin 'ligne' for display parent information.

->add('logistique', null, array('label' =>"Requ锚te Logistique ".ucfirst($this->getParent()->getSubject()->getType()),'required' => true,'disabled' => $disableLogistique))

But sorry i leaved this way because it took me too much time and backed to only one admin with borring customs on create view.
And i cant give much informations .
i Close this issue. Thank you anyway.

Yeah well, you can add an admin_code option here, can't you?

You probably give the rigth solution i think : add admin_code in child admin for field which reference to parent admin.

I confirm it s working.
For 2 admin on same entity with a child admin, put admin_code in child admin field which reference parent admin is the solution.
Thank you @greg0ire.

PS: Thanks too for the guy who made the exception for detect if attributes have the same access modifiers in each mutliples Admin classes.
Compile Error: Access level to MagasinBundle\Admin\Logistique2Admin::$admin_code must be public (as in class MagasinBundle\Admin\LogistiqueAdmin)

@yedprior consider contributing better docs or error messages that might help people in your situation.

Was this page helpful?
0 / 5 - 0 ratings