I want to use 1-n relation between tables.
I have three tables USER, PERMISSION and UserPermission as below
USER( Id INT auto_increment, FirstName VARCHAR(50) NOT NULL, LastName VARCHAR(50) NOT NULL, Description VARCHAR(255), Gender VARCHAR(6) NOT NULL, Email VARCHAR(255) NOT NULL, Address VARCHAR(255), Username VARCHAR(20) NOT NULL, Password VARCHAR(255) NOT NULL, UNIQUE(Username), UNIQUE(Email), PRIMARY KEY(Id) ) PERMISSION( Id INT auto_increment, Name VARCHAR(50) NOT NULL, PRIMARY KEY(Id), UNIQUE(Name) ) UserPermission( Priority INT auto_increment, UserId INT NOT NULL, Permission VARCHAR(50) NOT NULL, PRIMARY KEY(Priority), FOREIGN KEY(UserId) REFERENCES USER(Id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY(Permission) REFERENCES PERMISSION(Name) ON DELETE CASCADE ON UPDATE CASCADE )
I'm trying to add new user with following controller code:
$crud = new grocery_CRUD(); $crud->set_table('user'); $crud->set_subject('User'); $crud->columns('FirstName','LastName','Gender','Username'); $crud->fields('FirstName','LastName','Description','Gender','Email','Address','Permissions','Username','Password','ConfPassword'); $crud->display_as('FirstName','First Name'); $crud->display_as('LastName','Surname'); $crud->display_as('ConfPassword','Confirm Password'); $crud->field_type('Description','text'); $crud->field_type('Gender', 'enum',array('Male','Female')); $crud->field_type("Address", "text"); $crud->field_type("Password", "password"); $crud->field_type("ConfPassword", "password"); if ($crud->getState() == 'read') { $crud->field_type('Password', 'hidden'); $crud->field_type('ConfPassword', 'hidden'); } $crud->set_relation_n_n('Permissions', 'userpermission', 'permission', 'userid', 'permission', 'name','priority'); /*required field in add/edit*/ $crud->required_fields('FirstName','LastName','Gender','Email','Username','Password','ConfPassword'); $crud->unique_fields('Username','Email'); $crud->callback_before_insert(array($this,'unset_verification')); $crud->callback_before_update(array($this,'unset_verification')); $output = $crud->render(); $this->load->view('user/view/user',$output);
When I add new user it asks to select multiple permissions, but when I save it, the permissions data is not getting reflected in any of the tables mentioned above.
UserPermission table is blank. Can anybody help me to know what going wrong in this code??
Do i need to have Permissions field in USER table? If yes, what it's type should be?
And one more question, what is the priority field? (I just borrowed it from example given in GC). Is it needed? If yes, for what? Can I use Id instead of priority?