【问题标题】:get all groups from the Users - sfDoctrineGuardPlugin从用户获取所有组 - sfDoctrineGuardPlugin
【发布时间】:2011-09-25 10:53:58
【问题描述】:

在 sfDoctrineGuardPlugin 中是:

sfGuardUser:
  actAs: [Timestampable]
  columns:
    first_name: string(255)
    last_name: string(255)
    //
  indexes:
    is_active_idx:
      fields: [is_active]
  relations:
    Groups:
      class: sfGuardGroup
      local: user_id
      foreign: group_id
      refClass: sfGuardUserGroup
      foreignAlias: Users



sfGuardGroup:
  actAs: [Timestampable]
  columns:
    name:
      type: string(255)
      unique: true
    description: string(1000)
  relations:
    Users:
      class: sfGuardUser
      refClass: sfGuardUserGroup
      local: group_id
      foreign: user_id
      foreignAlias: Groups

sfGuardUserGroup:
  options:
    symfony:
      form:   false
      filter: false
  actAs: [Timestampable]
  columns:
    user_id:
      type: integer
      primary: true
    group_id:
      type: integer
      primary: true
  relations:
    User:
      class: sfGuardUser
      local: user_id
      onDelete: CASCADE
    Group:
      class: sfGuardGroup
      local: group_id
      onDelete: CASCADE

这是多对多的关系,我怎样才能获得所有用户组?

  • @method Doctrine_Collection
    getGroups() 返回 当前记录的“组”集合

我做:

$this->groups = $this->getUser()->getGuardUser()->getGroups();

这个回报:

 Doctrine_Collection 数据:Array( ) 

如何检查用户是否在组 TEST 中?

感谢您的帮助!

【问题讨论】:

    标签: php symfony1 doctrine symfony-1.4 sfdoctrineguard


    【解决方案1】:

    查看 sfGuardSecurityUser 类:https://github.com/Garfield-fr/sfDoctrineGuardPlugin/blob/master/lib/user/sfGuardSecurityUser.class.php

    if ($this->getUser()->hasGroup('TEST')) {
        //if user is on group TEST
    }//end if
    
    // get all groups
    
    $userGroups = $this->getUser()->getGroups();
    

    【讨论】:

    • 谢谢,但我使用 $userGroups 然后我有:“
       Doctrine_Collection data : Array( 0 : Object(sfGuardGroup) ) 
      ” 我该如何显示它? :) foreach 和 .. ?
    • @Tony Oriondo:如果您没有为用户设置组,getGroups 方法会返回一个空数组。您确定为您的用户设置了组吗?
    【解决方案2】:

    您可以尝试使用查询来查找您的用户组:

    $user_id = $this->getUser()->getGuardUser()->getId();
    $groups = Doctrine_core::getTable('sfGuardGroup')->create_query('g')->innerJoin('g.users u with u.id= ?', $user_id);
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      相关资源
      最近更新 更多