简体   繁体   中英

Edit Object Sonata admin bundle

I want to edit a sub object Pourcentage contained in the Parametre object (OneToOne association) but when I click edit, it redirects me to the edition of the Parametre and not the Pourcentage

I want to do this in my custom template :

<a href="{{ admin.generateObjectUrl('edit', parametre.pourcentage) }}" class="btn btn-sm btn-default edit_link" title="{{ 'action_edit'|trans({}, 'SonataAdminBundle') }}">
                        <i class="fa fa-pencil" aria-hidden="true"></i>
                        {{ 'action_edit'|trans({}, 'SonataAdminBundle') }}
                    </a>

Thank for you help

class ParametreAdmin extends AbstractAdmin
{
    protected $baseRoutePattern = 'parametre';

    protected function configureFormFields(FormMapper $formMapper)
    {
      $formMapper
            ->add("pourcentage", "sonata_type_admin") 
            ->add("mise", "sonata_type_admin");

      $this->preUpdate($formMapper);
    }

    public function preUpdate($object)
    {
        $entityCreate = $this->getSubject();   
        $user = $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser();
        $entityCreate->setUser($user); 
    }
}


class PourcentageAdmin extends AbstractAdmin
{
    protected $baseRoutePattern = 'pourcentage';

    protected function configureFormFields(FormMapper $formMapper)
    {
       $formMapper
            ->add("pourcentageMise", 'text');
    }  
}

First you do not need to call preUpdate from configureFormFields because the preUpdate will call before update! 1. I am not sure that I understand from where you click edit and etc, give me more information if this not work for you! 2. If you want to set user before update and persist you can do it like follow:

    class ParametreAdmin extends AbstractAdmin
{

protected $baseRoutePattern = 'parametre';

protected function configureFormFields(FormMapper $formMapper)
{

  $formMapper
        ->add("pourcentage", "sonata_type_admin") 
        ->add("mise", "sonata_type_admin");
   ;


}

 protected function configureListFields(ListMapper $list) {
  ..........................
    $list->add('_action', null, array(
        'actions' => array(
            'custom_show' => array('template' => 'YOURBundle:PATH:TEMPLATE_NAME.html.twig')
        )
    ));
}


protected function getUser(){
  return $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser();
}

public function prePersist($object)
{
    $user = $this->getUser;
    $object->setUser($user); 
}

 public function preUpdate($object)
{
    $user = $this->getUser;
    $object->setUser($user); 
}
}

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