caridy 2011-05-19 16:47:16 4453次浏览 10条回复 0 0 0

帮我分析个写法. 表A,用户表 表B,权限表 表C,权限分派表

我需要拉出来,用户未分派的权限列表. 目前我只能拉出来所有权限,然后拉出来用户的权限.再去排除. 这样好像挺麻烦的.如果用AR来实现的话怎么处理? 不会只能自己写SQL语句吧?用Find之类的,配合模型里面的关联,能不能实现

  • 回复于 2011-05-19 16:52 举报

    这么简单的,我就不回答了……

  • 回复于 2011-05-19 16:53 举报

    版主,把你写的rbac的教程拿出来吧!

  • 回复于 2011-05-19 16:53 举报

    楼上上让我感受到灰常悲催的情怀!...

  • 回复于 2011-05-19 16:55 举报

    那个不适用这个,这个是单纯的数据库查询。
    多对多关联,查询出没有被关联的项目么,和RBAC没有太大关系。

  • 回复于 2011-05-19 16:56 举报

    感谢 路人甲 提供参考.先帖出来再研究.

    public function getDiffItemTree ( $itemChildren )
    {
      $type = $this -> getType ();
      $typeItems = $diffItems = array ( );
      $typeItems = $this -> getAuthItems ( $type,'child','item_id,type,name' );
      $typeItems = $this -> getItemTree ( $typeItems );
      foreach ( $typeItems as $key => $value )
      {
        $diffItems [ $key ] = array_diff_assoc ( $typeItems[ $key ],$itemChildren[ $key ] );
      }
      return $diffItems;
    }
    
  • 回复于 2011-05-19 16:59 举报

    是不是这样的情况,直接写sql来查询,会比查出来,然后再循环排除会更适用一些?

  • 回复于 2011-05-19 17:04 举报

    没手写过SQL在Yii里了好久……

    User {{users}} (id ... );   Auth {{auths}} (id ... );  Relation {{relations}}(userId, authId); 
    

    这三那个表啊

    User::relations(){
      return array(
        'auths' => array(self::MANY_MANY, 'Auth', '{{relations}}(userId, authId)'),
      );
    }
    Auth::relations() {
      return array(
        'users' => array(self::MANY_MANY, 'User', '{{relations}}(authId, userId)'),
      );
    }
    

    然后查出所有没有被关联过的Auth么,就没啥难度了吧?

  • 回复于 2011-05-19 17:25 举报

    直接查询Auth表,如果没有user的就是没有被分配的。

  • 回复于 2011-05-19 20:20 举报

    找到解决方法了.帖出来共享:

    //实现本句:

    SELECT * from g_app_sys where id not IN ( SELECT app_id from g_user_app_role where user_id is null);
    if ($this->_haveapps === null) {
      $this->_haveapps = AppSys::model()->findAll(array(
        'with'=>array('userAppRoles'),//关联表的Model名
        'condition'=>'userAppRoles.user_id is null',
      )
      );
    }
    return $this->_haveapps;
    

    with的参数参考此地址:http://yiiframework.net/post/68/Active+Record+%E5%85%B3%E8%81%94

  • 回复于 2011-05-20 10:03 举报

    昨天晚上讨论时,说这种情况,用到left join 实际上效率并不高.建议大家再讨论一下具体怎么处理.
    我用到的地方,因为数据非常少,打算先取一一个值,在做为一个条件,取另一个值.So,大家用到的时候,要注意效率问题.

您需要登录后才可以回复。登录 | 立即注册