1

我正在尝试将一些用户保存在自定义管理表单中,并且我想将它们设置在特定组中,在sfGuardUserGroup.

因此,如果我刚刚创建的用户的 id 为 25,那么我希望表中的条目sfGuardUserGroup具有user_id25 和 a group_id88 是我想要添加这些用户的组 id。)

我可以在表单类或操作中执行此processForm操作吗?

我正在使用学说和 SF1.4

谢谢

4

2 回答 2

1

这应该做你需要的:

<?php

class AdminUserForm extends sfGuardUserForm
{
  public function configure()
  {
    //customise form...
  }

  public function save($con = null)
  {
    //Do the main save to get an ID
    $user = parent::save($con);

    //Add the user to the relevant group, for permissions and authentication
    if (!$user->hasGroup('admin'))
    {
      $user->addGroupByName('admin');
      $user->save();
    }

    return $user;
  }
}
于 2011-04-21T13:42:52.143 回答
0

如果您对所有创建的 sfGuardUser 都需要此行为,则应将此逻辑放入 sfGuardUser 类的模型中。[以下示例]

// sfGuardUser class
public function save(Doctrine_Connection $conn = null) {

    if (!$this->hasGroup('group_name'))
        $this->addGroupByName('group_name', $conn);

    parent::save($conn);

}

如果您仅在此特定表单上需要此功能,则应将逻辑放在表单中。向 processForm 操作添加逻辑是不正确的,因为您会将业务逻辑放在控制器中。

于 2011-04-21T13:01:55.680 回答