简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM