开发者

Need to have a user in more than one groups in cakePHP

开发者 https://www.devze.com 2023-01-15 19:02 出处:网络
Hello I am trying the ACL component in cakephp for my web application. The example on their website for using their ACL structure has a many to one relationship between groups and users. A user can be

Hello I am trying the ACL component in cakephp for my web application. The example on their website for using their ACL structure has a many to one relationship between groups and users. A user can belong to one group, while a group can have more than one users in it.

But I have a situation where I need to have a few users in more than one group. For that I had to change the table structure, as it would be a many to many. So I built an associative entity called "groups_users". Now I have baked all the controllers, models, and views.

I have added groups and users, and things are working fine. I have also added the components into the parent class, AppController. I have also generated the ACL tables ACOs, AROs and AROs_ACOs using the baking console.

So in the groups_users add view, I have a list of users and groups. I can choose the users and their groups, and on submit a record has to be technically created in my groups_users table.

But to my surprise, records are being inserted into aros_acos table which is weird. I believe there is some internal relation between users, groups and the acl tables. I tried to understand if there was any foreign key cascading between these tables, but nothing was mentioned in the config/schema/dbacl.sql file.

I am pasting my table structure for a better understanding of my problem.

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `acl_cake` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `acl_cake` ;

-- -----------------------------------------------------
-- Table `acl_cake`.`users`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `acl_cake`.`users` (
  `id` INT(11) NOT NULL AUTO_INCREMENT ,
  `username` VARCHAR(255) NOT NULL ,
  `password` CHAR(40) NOT NULL ,
  `created` DATETIME NULL DEFAULT NULL ,
  `modified` DATETIME NULL DEFAULT NULL ,
  PRIMARY KEY (`id`) ,
  UNIQUE INDEX (`username` ASC) );


-- -----------------------------------------------------
-- Table `acl_cake`.`groups`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `acl_cake`.`groups` (
  `id` INT(11) NOT NULL AUTO_INCREMENT ,
  `name` VAR开发者_开发知识库CHAR(100) NOT NULL ,
  `created` DATETIME NULL DEFAULT NULL ,
  `modified` DATETIME NULL DEFAULT NULL ,
  PRIMARY KEY (`id`) );


-- -----------------------------------------------------
-- Table `acl_cake`.`posts`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `acl_cake`.`posts` (
  `id` INT(11) NOT NULL AUTO_INCREMENT ,
  `title` VARCHAR(255) NOT NULL ,
  `body` TEXT NULL DEFAULT NULL ,
  `created` DATETIME NULL DEFAULT NULL ,
  `modified` DATETIME NULL DEFAULT NULL ,
  `user_id` INT(11) NOT NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `fk_posts_users1` (`user_id` ASC) ,
  CONSTRAINT `fk_posts_users1`
    FOREIGN KEY (`user_id` )
    REFERENCES `acl_cake`.`users` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);


-- -----------------------------------------------------
-- Table `acl_cake`.`widgets`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `acl_cake`.`widgets` (
  `id` INT(11) NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(100) NOT NULL ,
  `part_no` VARCHAR(12) NULL DEFAULT NULL ,
  `quantity` INT(11) NULL DEFAULT NULL ,
  PRIMARY KEY (`id`) );


-- -----------------------------------------------------
-- Table `acl_cake`.`groups_users`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `acl_cake`.`groups_users` (
  `id` INT NOT NULL ,
  `user_id` INT(11) NOT NULL ,
  `group_id` INT(11) NOT NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `fk_permissions_users` (`user_id` ASC) ,
  INDEX `fk_permissions_groups1` (`group_id` ASC) ,
  CONSTRAINT `fk_permissions_users`
    FOREIGN KEY (`user_id` )
    REFERENCES `acl_cake`.`users` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_permissions_groups1`
    FOREIGN KEY (`group_id` )
    REFERENCES `acl_cake`.`groups` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;


This may help get you pointed in the right direction or possibly give you another route to take. Simple Auth Users Has And Belongs To Many Groups CakePHP


I can choose the users and their groups, and on submit a record has to be technically created in my groups_users table.

Do you mean when you insert a record assigning a user to a group?

Does your application work as expected apart from the problem you describe?

It's difficult to be sure from your description, but it's possible that an aro_aco record has to be created for each user/group combination.

0

精彩评论

暂无评论...
验证码 换一张
取 消