如何在yii框架中使用不同的用户登录进行datadriven菜单和子菜单?

时间:2014-02-17 05:37:01

标签: php sql yii

CREATE TABLE mainmenu (
  menuid int(10) NOT NULL,
  mainmenuname varchar(50) NOT NULL,
  url varchar(100) NOT NULL,
  PRIMARY KEY (menuid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

这是主菜单

CREATE TABLE submenu (
  submenuid int(10) NOT NULL,
  menuid int(10) NOT NULL,
  submenuname varchar(50) NOT NULL,
  url varchar(100) NOT NULL,
  PRIMARY KEY (submenuid),
  KEY menuid (menuid),
  CONSTRAINT submenu_ibfk_1 FOREIGN KEY (menuid) REFERENCES mainmenu (menuid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

这是子菜单表

CREATE TABLE user_rights (
  user_id varchar(20) NOT NULL,
  submenuid int(3) NOT NULL,
  menuid int(3) NOT NULL,
  accessrole varchar(20) DEFAULT NULL,
  PRIMARY KEY (user_id,submenuid,menuid),
  KEY submenuid (submenuid),
  KEY menuid (menuid),
  CONSTRAINT user_rights_ibfk_1 FOREIGN KEY (user_id) REFERENCES user (user_id),
  CONSTRAINT user_rights_ibfk_2 FOREIGN KEY (submenuid) REFERENCES submenu (submenuid),
  CONSTRAINT user_rights_ibfk_3 FOREIGN KEY (menuid) REFERENCES mainmenu (menuid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

这是用户权限表

CREATE TABLE user (
  username varchar(20) NOT NULL,
  password varchar(20) NOT NULL,
  user_id varchar(20) NOT NULL,
  accessrole varchar(20) NOT NULL,
  PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

这是用户表。

如何从与用户权限tbl相关联的数据库中加载菜单和子菜单?

2 个答案:

答案 0 :(得分:0)

不是使用单独的菜单和子菜单表,而是创建一个列为int'parentID'的单个表。

创建一个迭代函数来处理/显示每个子菜单。

<code>
private function processBranch($branch) {
$criteria = new CDbCriteria;
$criteria->addCondition('parentID = ' . $branch);
$criteria->addCondition('accessrole = ' . $this->access);

$menuitems = Menu::model()->findAll($criteria);
if(!empty($menuitems)) {
    foreach($menuitems as $item) {
        // EcHo menu content here

        $this->processBranch($item->id);
    }
}
}

使用processBranch(0)显示根菜单

答案 1 :(得分:0)

数据建模的问题是每个“mainmenu”必须至少有一个“子菜单”。这真的需要吗?