【问题标题】:Is there any way to determine current action (create or edit) in Sonata\AdminBundle\Admin\Admin::configureFormFields()?有没有办法确定 Sonata\AdminBundle\Admin\Admin::configureFormFields() 中的当前操作(创建或编辑)?
【发布时间】:2013-07-23 23:21:49
【问题描述】:

我想为 Sonata Admin Bundle 中的创建和编辑操作创建不同的字段配置。

除了在Sonata\AdminBundle\Admin\Admin::configureFormFields()中检查$this->getSubject()->getId()之外,还有什么方法可以确定吗?

【问题讨论】:

    标签: symfony sonata-admin


    【解决方案1】:

    与:

    if($this->getRequest()->get($this->getIdParameter()) == null){
       // create
    } else {
       // edit
    }
    

    【讨论】:

    • 谢谢,但对我来说,这与检查实体对象的 ID 字段一样肮脏 :)
    • 当管理员通过 sonata_type_collection 加载时,这个总是返回 'create'
    【解决方案2】:

    您也可以这样做:

    protected function configureFormFields(FormMapper $formMapper) {
      if ($this->isCurrentRoute('create')) {
        // CREATE
      }
      else {
        // EDIT
      }
    }
    

    【讨论】:

    • 谢谢!它看起来更好:)
    【解决方案3】:

    我用这个:

    $creationMode = ($this->id($this->getSubject()))?(false):(true);
    if ($creationMode){
     //Ok
    }
    

    【讨论】:

      【解决方案4】:

      您也可以这样做:

      protected function configureFormFields(FormMapper $formMapper) {
        if ($this->isCurrentRoute('create')) {
          // CREATE
        }
        else {
          // EDIT
        }
      }
      

      【讨论】:

        【解决方案5】:

        在版本 3.x 的奏鸣曲管理中

          if ($this->isCurrentRoute('create')) {
            // CREATE
          }
          else {
            // EDIT
          }
        

        在 3.x 版本之前的奏鸣曲管理中使用:

          $subject = $this->getSubject();
          if ($subject->isNew()) { 
            // CREATE
          }
          else {
            // EDIT
          }
        

        【讨论】:

          【解决方案6】:
          public function getAction(): ?string
          {
              if (! $this->getRequest()) {
                  return null;
              }
              $pathArray = \explode('/', $this->request->getPathInfo());
          
              return \end($pathArray);
          }
          

          【讨论】:

            猜你喜欢
            • 2016-10-02
            • 1970-01-01
            • 1970-01-01
            • 2014-12-04
            • 1970-01-01
            • 2015-05-27
            • 1970-01-01
            • 2018-12-25
            • 2010-12-20
            相关资源
            最近更新 更多