Php symfony管理生成器窗体对象

Php symfony管理生成器窗体对象,php,forms,symfony1,admin-generator,Php,Forms,Symfony1,Admin Generator,嘿,伙计们,我用了Symfony管理生成器作为一个模块 一切正常,但是当我的模型的表单被实例化时,我需要传递我自己的选项 我可以通过重写myModuleActions.class.php中的executeNew、executeCreate函数(它扩展了mymoduleautoaction)来实现这一点 但我希望有一个更整洁的解决方案 也许重写其中一个配置类是一种方法。我基本上需要将当前的sf\u user对象($this->getUser)添加为表单的“sf\u user”选项,以避免在myMo

嘿,伙计们,我用了
Symfony管理生成器
作为一个模块

一切正常,但是当我的模型的表单被实例化时,我需要传递我自己的选项

我可以通过重写
myModuleActions.class.php
中的executeNew、executeCreate函数(它扩展了mymoduleautoaction)来实现这一点

但我希望有一个更整洁的解决方案

也许重写其中一个配置类是一种方法。我基本上需要将当前的
sf\u user
对象(
$this->getUser
)添加为表单的“
sf\u user
”选项,以避免在
myModuleForm
中使用
sfContext


有什么想法吗?

欢迎来到Stack Overflow,Jolly 18

我只会使用sfContext。例如,在我的应用程序中,我有一个子窗体,用于创建新的便笺对象并将用户分配给它。在我的表单的
configure()
中,我有:

$new_note->setAuthor(sfContext::getInstance()->getUser()->getUsername());

我明白了,因为它使“表单和上下文之间有了很大的耦合,使得测试和可重用性更加困难。”但在实践中。。。这很好,我可以继续前进。

我已经面对这个问题有一段时间了,但是symfony总是用一些我不知道的简洁代码让我感到惊讶

我假设您使用的是SFPROPERPLUGIN,非常标准,如果您签出缓存中生成的代码(注意:一旦您尝试从浏览器打开模块,此代码将可用,因此首先尝试查看它,这样我们就不会遇到麻烦:p)您可能会看到类似以下内容:

cache/{application\u name}(通常是前端或后端)/dev(environment)/autoModule\u name(在此处查找模块)/

  • 解放党
  • 行动
action文件夹包含一个action.class.php文件,该文件定义生成器生成的所有操作(executeNew、Edit、Create、Update等)。如果查看executeNew和executeEdit的实现,可以看到它们要求配置instace显示实际表单,下面是一个示例:

  public function executeNew(sfWebRequest $request)
  {
    $this->form = $this->configuration->getForm();
    $this->PaymentOrder = $this->form->getObject();
  }
配置变量包含我前面提到的lib文件夹中定义的配置类的实例。该类调整表单以满足对象需要(通常通过设置新的对象实例)

因此,神奇的是,您在模块中看到的类是从缓存中的类扩展而来的,因此,从纯逻辑上讲,如果您修改主模块/lib文件夹中的
getForm()
方法以满足您的需要,您就不必通过在不应该的地方获取user valuer来破解表单


希望这有帮助

如果模块是使用管理生成器生成的:

apps/backend/modules/book/actions/actions.class.php

修改:在

executeEdit(){

//leave rest unchanged

$values=array('activity_id'=>$activity_id, 'book_id'=>$book_id, 'todo_id'=>$todo_id, 'user_id'=>$this->getUser()->getGuardUser()->getId());


    $this->form = new TabelBooksForm($TabelBooks, $values);
}
executeNew(){

//leave rest unchanged

$values=array('activity_id'=>$activity_id, 'book_id'=>$book_id, 'todo_id'=>$todo_id, 'user_id'=>$this->getUser()->getGuardUser()->getId());

    $this->form = new TabelBooksForm(array(), $values);
}
修改:在

executeEdit(){

//leave rest unchanged

$values=array('activity_id'=>$activity_id, 'book_id'=>$book_id, 'todo_id'=>$todo_id, 'user_id'=>$this->getUser()->getGuardUser()->getId());


    $this->form = new TabelBooksForm($TabelBooks, $values);
}
executeNew(){

//leave rest unchanged

$values=array('activity_id'=>$activity_id, 'book_id'=>$book_id, 'todo_id'=>$todo_id, 'user_id'=>$this->getUser()->getGuardUser()->getId());

    $this->form = new TabelBooksForm(array(), $values);
}
在TabelBooksForm.class.php中

public function configure()
  {

   if ($this->isNew()) {
    $this->setWidget('book_id', new sfWidgetFormInputHidden());
    $this->setDefault('book_id', $this->getOption('book_id'));    

    $this->setWidget('activity_id', new sfWidgetFormInputHidden());
    $this->setDefault('activity_id', $this->getOption('activity_id'));    

    $this->setWidget('todo_id', new sfWidgetFormInputHidden());
    $this->setDefault('todo_id', $this->getOption('todo_id'));  
  }
}
重写myModuleGeneratorConfiguration中的“sfModelGeneratorConfiguration::getForm”方法可能会起作用,但仍然需要我使用sfContext来获取当前的sf_用户对象