Php 将用户ID从设置默认值传递到当前用户
一个简短的问题。我正在使用symfony1.4和ORM和sfGuardDoctrinePlugin。我有一个名为“任务”的symfony表单。我希望userId字段(FK to Id fied if user table)默认设置为当前登录的用户。我怎样才能做到这一点Php 将用户ID从设置默认值传递到当前用户,php,symfony-1.4,sfguard,Php,Symfony 1.4,Sfguard,一个简短的问题。我正在使用symfony1.4和ORM和sfGuardDoctrinePlugin。我有一个名为“任务”的symfony表单。我希望userId字段(FK to Id fied if user table)默认设置为当前登录的用户。我怎样才能做到这一点 //apps/myapp/modules/task/actions class taskActions extends sfActions { public function executeNew(sfWebRequest
//apps/myapp/modules/task/actions
class taskActions extends sfActions
{
public function executeNew(sfWebRequest $request)
{
$this->form = new taskForm();
}
public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST));
$this->form = new taskForm();
$this->processForm($request, $this->form);
$this->setTemplate('new');
}
}
回答这个问题有点棘手,不知道您是如何通过操作或通过
$form->configure()
设置表单的,但是您可以使用以下方法访问当前用户id:
$currentUserId = sfContext::getInstance()->getUser()->getGuardUser()->getId();
--更新--
根据您的更新,taskForm
似乎不是基于模型对象,否则您将通过构造函数传递对象,因此它必须是自定义表单。有两种方法可以对此cat进行蒙皮,您可以通过构造函数传递用户对象,也可以通过公共访问器设置值,如下所示:
class taskForm
{
protected $user;
public function setUser($user)
{
$this->user = $user;
}
public function getUser()
{
return $this->user;
}
public function configure()
{
// This should output the current user id which demonstrates that you now
// have access to user attributes in your form class
var_dump($this->getUser()->getGuardUser()->getId());
}
}
要设置它:
public function executeNew(sfWebRequest $request)
{
$this->form = new taskForm();
$this->form->setUser($this->getUser());
}
另一种方法是直接通过构造函数传递用户对象,然后可以在表单中使用
$this->getObject()->getUser()
引用它,尽管我不建议这样做,因为它会在用户上下文中强制taskForm。很抱歉没有正确地开始这个问题。下面是通过操作初始化表单的代码。我真正想要的是将当前用户传递给表单。不用担心。用你的方式更新帖子。