symfony 管理生成器表单对象

发布于 2024-10-08 02:35:02 字数 420 浏览 0 评论 0原文

大家好,我已经在模块中使用了 Symfony 管理生成器

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

我可以通过重写 myModuleActions.class.php(扩展了 myModuleAutoActions)中的executeNew、executeCreate 函数来自己完成此操作。

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

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

有什么想法吗?

Hey guys, I've used the Symfony admin generator for a module.

Everything is working, but when the form for my model is instantiated, I need to pass in my own option.

I could do this myself by overriding the executeNew, executeCreate functions in myModuleActions.class.php (which extends myModuleAutoActions).

But I was hoping for a neater solution?

Perhaps overriding one of the configuration classes is the way to go. I basically need to add the current sf_user object ($this->getUser) as an "sf_user" option for the form, to avoid using sfContext in the myModuleForm.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪心 2024-10-15 02:35:02

欢迎来到 Stack Overflow,jolly18。

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

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

我看到 本书称此为“最快但丑陋的方法”,因为它使“表单和上下文之间存在很大的耦合,使测试和可重用性变得更加困难”。但实际上......这很有效,我可以继续前进。

Welcome to Stack Overflow, jolly18.

I would just use sfContext. For example, in my app, I have a subform that creates a new Note object and assigns the user to it. In my form's configure() I have:

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

I see the book calls this "The fastest but ugly way" because it makes "a big coupling between the form and the context, making the testing and reusability more difficult." But in practice... this works well and I can move on.

时光无声 2024-10-15 02:35:02

如果模块是使用 admin-generator 生成的:

apps/backend/modules/books/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);
}

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

if module was generated using admin-generator :

in apps/backend/modules/books/actions/actions.class.php

modify: in

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

modify: in

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

in 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'));  
  }
}
爱情眠于流年 2024-10-15 02:35:02

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

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

cache/{application_name}(通常是前端或后端)/dev(enviromnemt)/autoModule_name(在此处查找模块)/:

  • lib
  • action

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

  public function executeNew(sfWebRequest $request)
  {
    $this->form = $this->configuration->getForm();
    $this->PaymentOrder = $this->form->getObject();
  }

配置变量包含我之前提到的lib文件夹中定义的配置类的实例。该类调整表单以适应对象需求(通常通过设置新的对象实例)。

神奇之处在于,您在模块中看到的类是从缓存中的类扩展而来的,因此,如果您修改主 module/lib 文件夹中的 getForm() 方法来满足您的需求,那么通过纯粹的逻辑,您不必通过在不应该的地方获取用户评估器来破解表单。

希望这有帮助!

i've been facing this problem for a while but symfony always surprises me with some neat code that i was not aware of.

I assume you'r using sfPropelPlugin, quite standar, if you checkout the code generated in cache (note: this code will be available once you tried to open the module from the browser, so firts try to look at it so we dont get in trouble :P) you may see something like:

cache/{application_name}(generally frontend or backend)/dev(enviromnemt)/autoModule_name( look here for the module)/:

  • lib
  • action

The action folder contains an action.class.php file that defines all actions generated by the generator (executeNew, Edit, Create, Update, etc). If you look a the implementation of executeNew and executeEdit, you can see that they ask a configuration instace the actual form to display, here is an example:

  public function executeNew(sfWebRequest $request)
  {
    $this->form = $this->configuration->getForm();
    $this->PaymentOrder = $this->form->getObject();
  }

The configuration var containt an instance of a configuration class defined in the lib folder i mentioned earlier. That class tweaks the form to fit the object needs (generally by setting a fresh object instance).

So here comes the magic, the classes you see in your module extend from those in cache, so by pure logic, if you modifi the getForm() method in the main module/lib folder to fit your needs, you wont have to hack forms by getting user valuer where you shouldn't.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文