我正在创建自己的博客引擎来学习 Symfony,我有一个问题:
在为博客文章生成的管理页面中,我有一个作者下拉列表,以指示 author_id。
我想隐藏该下拉列表,并在创建帖子时将 author_id 设置为当前登录用户的 id(但不是在编辑时)
我怎样才能做到这一点?
编辑 我已经尝试过那些:
$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()
解决 !
最终代码是:
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);
}