【问题标题】:Symfony Admin Generator Doctrine executeCreateSymfony Admin Generator 学说 executeCreate
【发布时间】:2011-06-11 06:28:29
【问题描述】:

感谢您对此的任何帮助,我对 Symfony 框架非常陌生,所以只是想了解一下它。

我希望能够从管理区域截取提交的表单并修改数据。

这是我到目前为止所得到的(基本形式)..

/apps/backend/modules/proposition/actions/action.class.php

class propositionActions extends autoPropositionActions {

  public function executeCreate(sfWebRequest $request) {
    // modify the name
    $name = $request->getParameter('name');
    $name = $name . ' is an idiot';
    $request->setParameter('name', $name);

    return parent::executeCreate($request);
  }

}

我的表单确实包含一个名称字段:

/apps/backend/modules/proposition/config/generator.yml

generator:
  class: sfDoctrineGenerator
  param:
    model_class:           Proposition
    theme:                 admin
    non_verbose_templates: true
    with_show:             false
    singular:              ~
    plural:                ~
    route_prefix:          proposition
    with_doctrine_route:   true
    actions_base_class:    sfActions

    config:
      actions: ~
      form:
        display: [name, icon, overview, published]

我不确定这是否是您需要查看的文件,但它肯定在 HTML 中:

<input type="text" id="proposition_name" name="proposition[name]">

当我提交表单时,它只会保存我的名字。我希望它保存我的名字附加“是个白痴”。

非常感谢

【问题讨论】:

    标签: symfony1 doctrine admin symfony-1.4


    【解决方案1】:

    为什么要在你的行动中这样做?我认为最合适的地方是形式本身。

    您可以通过添加 update*Column 方法来修改任何列的值:

    class PropositionForm extends BasePropositionForm
    {
      public function updateNameColumn($name)
      {
        return $name . ' is an idiot';
      }
    }
    

    注意:如果您不希望在您的表单使用的其他地方附加“白痴”字符串,您可以将原始表单子类化并在那里添加您的方法(即在 AdminPropositionForm 中)。

    您可以通过在模块的配置类 (propositionGeneratorConfiguration) 中重载 getFormClass() 方法来更改管理员生成的模块使用的表单。它应该返回您要使用的表单类的名称。

    注意 2: 您必须以不同的方式访问表单值:

    $proposition = $request->getParameter('proposition', array());
    $name = $proposition['name'];
    

    【讨论】:

    • 谢谢 kuba,你的第二条笔记帮了大忙。虽然一旦我扩展了 processForm() 我就使用它: $parms = $request->getParameter($form->getName()); $name = $parms['name'];
    • @Peter:你是对的。使用 $form->getName() 比显式使用名称更好。
    【解决方案2】:

    我认为你在正确的轨道上彼得,但修改 $request 为时已晚,无法产生任何效果。

    您可以对the "doClean" part of the Form's validator 中的入站数据进行此类修改。

    或者,如果您有特殊处理要做,覆盖生成的processForm() 函数可能更有意义。只需将其从 cache/frontend/dev/modules/autoProposition/actions/actions.class.php 复制到您的 apps/backend/modules/proposition/actions/action.class.php 并开始破解。

    【讨论】:

    • 谢谢 Nathan,processForm() 是我正在寻找的函数。我已经扩展了它,它现在可以按我的意愿工作。我想知道 autoPropositionActions 类在哪里,看看这个给了我更多的机会:)
    • 改变 processform 中字段值的语法是什么?我正在尝试将 author_id 设置为当前登录用户的 id...
    • @Manu 看起来您已经在 the question you asked about that 上获得帮助。祝你好运。
    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2011-01-14
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多