sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Get parent admin class from a defined child admin #5318

Closed filipe-carvalho closed 4 years ago

filipe-carvalho commented 5 years ago

Hello Dear Comunity I had implemented a child admin class based on documentation of sonata (SEE HERE)

Imagine this EXAMPLE:

ENTITY ( 2 ENTITIES)

Wizard:

class Wizard{

  /**
   * @ORM\Column(type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @ORM\OneToMany(targetEntity="App\Entity\WizardChild", mappedBy="wizard")
   */
  private $childs;
}

Wizard Child


class WizardChild{

  /**
   * @ORM\Column(type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @ORM\ManyToOne(targetEntity="App\Entity\Wizard", inversedBy="childs")
   */
  private $wizard;
}

SERVICES

Parent Admin Service:

app.admin.wizard:
    class: App\Admin\WizardAdmin
    arguments: [~, App\Entity\Wizard, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, label: 'wizard' }
    calls:
        - [ addChild, ['@app.admin.wizard.child', 'wizard']]

Child Admin Service:

app.admin.wizard.child:
    class: App\Admin\WizardChildAdmin
    arguments: [~, App\Entity\WizardChild, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, label: 'wizard child' }

ADMIN CLASSES

In the parent admin class of the configureform method i add the field that references the child admin ( from my entity i can acess the wizardchild entity based on the property childs):

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\Type\ModelType;

class WizardAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('childs', ModelType::class)
        ;
    }
}

Imagine that in my wizard child admin i want to acess the parent admin like this

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\Type\ModelType;

class WizardChildAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
       dump($this->getRoot());   -> THIS RESULTS ON (WizardChildAdmin) instead of (WizardAdmin)

      .... added some fields of my entity
    }
}

PROBLEM

the problem is that ever time i click to add a new wizard child from my wizard admin the dump of the root stills gives me my class instead of parent , why this is happening ?

Could this be a bug or i am doing something wrong ? How can i acess the parent admin class from my child admin class ???

Thanks in advance :)

Tulis2 commented 5 years ago

+1

kunicmarko20 commented 5 years ago

@Tulis2 instead of "+1" what about debugging this and providing a PR?

franmomu commented 5 years ago

Hi, maybe calling $this->getParent()?

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

HeldVomErdbeerfeld commented 3 years ago

I'm Struggeling with the same Problem.

Found out, that when using ModelType the following GET Parameters are passed to request code pcode uniqid and puniqid but neither pcode nor puniqid is used anywhere else in code.

https://github.com/sonata-project/SonataAdminBundle/blob/d4369373d08ee33c2e789ae0cb25872237e85482/src/Route/DefaultRouteGenerator.php#L102-L103

Shouldnt those be read in CRUDController::configureAdmin() ?