【问题标题】:Restrict choices shown in symfony admin generator edit multi-select限制 symfony 管理生成器中显示的选项编辑多选
【发布时间】:2011-04-15 13:20:47
【问题描述】:

我正在使用 sfGuardUser 模块的管理生成器。 generator.yml 文件的编辑部分如下所示:

edit:
  title: Editing User "%%username%%"
  display:
    "User":  [first_name, last_name, email_address, username, password, password_again]
    "Permissions and groups": [is_active, groups_list, sites_list]

现在,并非每个用户都可以访问此表单,只有站点管理员允许站点管理员创建和更新他们自己的用户。 UserSite 之间存在多对多关系。每个站点管理员也是一个用户,因此具有一组关联的站点。

我希望sites_list 不显示所有站点,而是仅显示与站点管理员关联的站点,从而确保站点管理员无法将她自己的用户之一放入与管理员无关的站点。

在我看来,我需要用其他东西替换 sites_list 来执行此操作,但我不知道在哪里以及如何进行此更改。

【问题讨论】:

    标签: symfony1 sfdoctrineguard


    【解决方案1】:

    我想到的唯一方法是从自动生成的表单中更改 sites_lists 小部件。例如,在您的情况下,您可以执行以下操作:

    <!-- SitesTable -->
    public function getByUser($userId){
         //create your query to find all sites from that user
          $userSites = $this->createQuery()->...
                            ->where('user_id = ?', $userId);
    
          //create the array
          $choices = array();
          foreach ( $userSites as $site ) {
              $choices[$site->getId()] = $site->getName();
          }
    
          return $choices;
    }
    
    <!-- sfGuardUserForm -->
    class sfGuardUserForm extends BaseSfGuardUserForm{
        public function configure() {
          //unset the old sites_list
          unset($this['sites_list']);
    
          //obtain the user id (depends on how it's implemented, i'm not using sfGuard)
          $userId = sfContext::getInstance()->getUser()->getId(); 
    
          $choices = Doctrine::getTable('Sites')->getByUser($userId);
    
          //set the new widget filtered
          $this->setWidget('sites_list', new sfWidgetFormChoice(array('choices' => $choices)));
          $this->setValidator('sites_list', new sfValidatorChoice(array('choices' => array_keys($choices))));
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 2011-06-21
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多