简体   繁体   English

编辑对象Sonata管理员捆绑包

[英]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 我想编辑包含在Parametre对象(OneToOne关联)中的子对象Pourcentage,但是当我单击Edit时,它会将我重定向到Parametre的版本,而不是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! 首先,您不需要从configureFormFields调用preUpdate,因为preUpdate将在更新之前调用! 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! 1.我不确定您从何处单击“编辑”等内容,如果对您不起作用,请给我更多信息! 2. If you want to set user before update and persist you can do it like follow: 2.如果要在更新之前设置用户并保留,则可以执行以下操作:

    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); 
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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