phpsymfonysonata-adminsymfony-sonatasonata-user-bundle

sonata Admin current entity


How can i access the id of a sonata admin entity. suppose i have an entity EndPoint which have a function getProject(), how can i get the id of that project. I tried the code bellow but this gave me the following error: Attempted to call an undefined method named "getProject" of class "ContainerAgbGotv\srcApp_KernelDevDebugContainer".

class EndPointAdmin extends AbstractAdmin{
protected function configureFormFields(FormMapper $form)
{   $container = $this->getConfigurationPool()->getContainer();
    $em = $container->getProject();
    
    $array = [];
    foreach ($em as $ems) {
        if (!empty($ems->getEnv())) {
            $array[$ems->getEnv()] = $ems->getEnv();
        }}
    $result = array_unique($array);
    $form
        
        ->add('name',ChoiceType::class, [
            'choices'=> $result,
            'placeholder' => 'Choose an option',

            'required' => false
        ])
        ->add('ipAdress',TextType::class)
        ->add('port',IntegerType::class)
               ;
}

thanks for helping.


Solution

  • In an admin you will have access to

    $this->getSubject()

    which will return you the current entity for that admin. It's worth noting configureFormFields is used for both creating and editing an entity so getSubject() may return null.