Symfony 管理生成器:保存前添加用户 ID [英] Symfony admin generator : add user id before saving

查看:19
本文介绍了Symfony 管理生成器:保存前添加用户 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自己的博客引擎来学习 Symfony,我有一个问题:

I'm creating my own blog engine to learn Symfony, and I have a question :

在为博客文章生成的管理页面中,我有一个作者下拉列表,用于指示 author_id.

In the generated administration pages for a blog post, I have a drop-down list of authors, to indicate the author_id.

我想隐藏那个下拉列表,并在创建帖子时(但不是在编辑时)将 author_id 设置为当前登录用户的 ID

I'd like to hide that drop-down list, and set the author_id to the id of the current logged-in user when the post is created (but not when it is edited)

我怎样才能做到这一点?

How can I accomplish that ?

编辑我试过那些:

$request->setParameter(sprintf("%s[%s]", $this->form->getName(), "author_id"), $this->getUser()->getAttribute("user_id"));
$request->setParameter("content[author_id]", $this->getUser()->getAttribute("user_id"));
$request->setParameter("author_id", $this->getUser()->getAttribute("user_id"));
$request->setParameter("author_id", 2);
$request->setParameter("content[author_id]", 2);
$request->setParameter("author_id", "2");
$request->setParameter("content[author_id]", "2");

在 processForm() 和 executeCreate() 中

In processForm() and executeCreate()

已解决!

最终代码是:

  public function executeCreate(sfWebRequest $request)
  {
    $form = $this->configuration->getForm();
    $params = $request->getParameter($form->getName());
    $params["author_id"] = $this->getUser()->getGuardUser()->getId();;
    $request->setParameter($form->getName(), $params);

    parent::executeCreate($request);

  }

推荐答案

覆盖操作文件中的 executeCreate 函数.将post数据绑定到表单时,将当前用户的id合并到表单中.

Override the executeCreate function in the actions file. When binding post data to the form, merge the current user's id into it.

第二次更新

我做了一些实验,效果很好:

I did some experimenting, and this works:

class fooActions extends autoFooActions
{
  public function executeCreate(sfWebRequest $request)
  {
    $form = $this->configuration->getForm();
    $params = $request->getParameter($form->getName());
    $params["author_id"] = 123;
    $request->setParameter($form->getName(), $params);

    parent::executeCreate($request);
  }
}

这篇关于Symfony 管理生成器:保存前添加用户 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆